Skip to content

Commit 3fea5c6

Browse files
author
Nikos M
committed
v.2.6.1
parse PEG/BNF in pre_proces method as well fix nodejs loading
1 parent 7bf3093 commit 3fea5c6

8 files changed

+84
-38
lines changed

api-reference.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__For node:__
66

77
```javascript
8-
CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
8+
CodeMirrorGrammar = require('build/codemirror_grammar.js');
99
```
1010

1111
__For browser:__
@@ -20,7 +20,7 @@ __For browser:__
2020
__Method__: `clone`
2121

2222
```javascript
23-
cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
23+
cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
2424
```
2525

2626
Clone (deep) a `grammar`
@@ -32,7 +32,7 @@ Utility to clone objects efficiently
3232
__Method__: `extend`
3333

3434
```javascript
35-
extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
35+
extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
3636
```
3737

3838
Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
@@ -44,19 +44,19 @@ This way arbitrary `dialects` and `variations` can be handled more easily
4444
__Method__: `pre_process`
4545

4646
```javascript
47-
CodeMirrorGrammar.pre_process( grammar );
47+
pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
4848
```
4949

5050
This is used internally by the `CodeMirrorGrammar` Class `parse` method
51-
In order to pre-process, in-place, a `JSON grammar`
52-
to transform any shorthand configurations to full object configurations and provide defaults.
51+
In order to pre-process a `JSON grammar` (in-place) to transform any shorthand configurations to full object configurations and provide defaults.
52+
It also parses `PEG`/`BNF` (syntax) notations into full (syntax) configuration objects, so merging with other grammars can be easier, if needed.
5353

5454

5555

5656
__Method__: `parse`
5757

5858
```javascript
59-
parsedgrammar = CodeMirrorGrammar.parse( grammar );
59+
parsed_grammar = CodeMirrorGrammar.parse( grammar );
6060
```
6161

6262
This is used internally by the `CodeMirrorGrammar` Class

beeld.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ tasks =[{}]
5050

5151
"@@ROOT@@" = "this"
5252
"@@EXPORTS@@" = "exports"
53-
"@@VERSION@@" = "2.6.0"
53+
"@@VERSION@@" = "2.6.1"
5454
"@@MODULE_NAME@@" = "CodeMirrorGrammar"
5555

5656
@

build/codemirror_grammar.js

+46-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
33
* CodeMirrorGrammar
4-
* @version: 2.6.0
4+
* @version: 2.6.1
55
*
66
* Transform a grammar specification in JSON format, into a syntax-highlight parser mode for CodeMirror
77
* https://github.com/foo123/codemirror-grammar
@@ -36,7 +36,7 @@ else if ( !(name in root) )
3636
"use strict";
3737
/**
3838
* EditorGrammar Codebase
39-
* @version: 2.6.0
39+
* @version: 2.6.1
4040
*
4141
* https://github.com/foo123/editor-grammar
4242
**/
@@ -1934,6 +1934,39 @@ function get_block_types( grammar, the_styles )
19341934
return blocks;
19351935
}
19361936

1937+
function preprocess_and_parse_grammar( grammar )
1938+
{
1939+
var processed = {}; // for recursive references
1940+
grammar.Lex = grammar.Lex || {}; grammar.Syntax = grammar.Syntax || {};
1941+
grammar = preprocess_grammar( grammar );
1942+
if ( grammar.Parser && grammar.Parser.length )
1943+
{
1944+
iterate( function process( i, T ) {
1945+
var id = T[ i ], t, token, type, tokens;
1946+
if ( processed[id] ) return;
1947+
if ( T_ARRAY & get_type( id ) )
1948+
{
1949+
// literal n-gram as array
1950+
t = id; id = "NGRAM_" + t.join("_");
1951+
if ( !grammar.Syntax[ id ] ) grammar.Syntax[ id ] = {type:"ngram", tokens:t};
1952+
}
1953+
token = get_backreference( id, grammar.Lex, grammar.Syntax );
1954+
if ( T_STR & get_type( token ) )
1955+
{
1956+
token = parse_peg_bnf_notation( token, grammar.Lex, grammar.Syntax );
1957+
token = grammar.Lex[ token ] || grammar.Syntax[ token ] || null;
1958+
}
1959+
if ( token )
1960+
{
1961+
processed[id] = token;
1962+
type = token.type ? tokenTypes[ token.type[LOWER]( ).replace( dashes_re, '' ) ] || T_SIMPLE : T_SIMPLE;
1963+
if ( T_COMPOSITE & type ) iterate( process, 0, token.tokens.length-1, token.tokens );
1964+
}
1965+
}, 0, grammar.Parser.length-1, grammar.Parser );
1966+
}
1967+
return grammar;
1968+
}
1969+
19371970
function parse_grammar( grammar )
19381971
{
19391972
var RegExpID, tokens,
@@ -3396,7 +3429,7 @@ var Folder = {
33963429
/**
33973430
*
33983431
* CodeMirrorGrammar
3399-
* @version: 2.6.0
3432+
* @version: 2.6.1
34003433
*
34013434
* Transform a grammar specification in JSON format, into a syntax-highlight parser mode for CodeMirror
34023435
* https://github.com/foo123/codemirror-grammar
@@ -3406,7 +3439,7 @@ var Folder = {
34063439

34073440

34083441
// codemirror supposed to be available
3409-
var $CodeMirror$ = CodeMirror || { Pass : { toString: function(){return "CodeMirror.Pass";} } },
3442+
var $CodeMirror$ = 'undefined' !== typeof CodeMirror ? CodeMirror : { Pass : { toString: function(){return "CodeMirror.Pass";} } },
34103443
// used for autocompletion
34113444
RE_W = /[\w$]/, by_score = function( a, b ) { return b.score-a.score }
34123445
;
@@ -3719,7 +3752,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
37193752
* __For node:__
37203753
*
37213754
* ```javascript
3722-
* CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
3755+
* CodeMirrorGrammar = require('build/codemirror_grammar.js');
37233756
* ```
37243757
*
37253758
* __For browser:__
@@ -3731,14 +3764,14 @@ function get_mode( grammar, DEFAULT, CodeMirror )
37313764
[/DOC_MARKDOWN]**/
37323765
var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {
37333766

3734-
VERSION: "2.6.0",
3767+
VERSION: "2.6.1",
37353768

37363769
// clone a grammar
37373770
/**[DOC_MARKDOWN]
37383771
* __Method__: `clone`
37393772
*
37403773
* ```javascript
3741-
* cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
3774+
* cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
37423775
* ```
37433776
*
37443777
* Clone (deep) a `grammar`
@@ -3752,7 +3785,7 @@ var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {
37523785
* __Method__: `extend`
37533786
*
37543787
* ```javascript
3755-
* extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
3788+
* extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
37563789
* ```
37573790
*
37583791
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
@@ -3766,21 +3799,21 @@ var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {
37663799
* __Method__: `pre_process`
37673800
*
37683801
* ```javascript
3769-
* CodeMirrorGrammar.pre_process( grammar );
3802+
* pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
37703803
* ```
37713804
*
37723805
* This is used internally by the `CodeMirrorGrammar` Class `parse` method
3773-
* In order to pre-process, in-place, a `JSON grammar`
3774-
* to transform any shorthand configurations to full object configurations and provide defaults.
3806+
* In order to pre-process a `JSON grammar` (in-place) to transform any shorthand configurations to full object configurations and provide defaults.
3807+
* It also parses `PEG`/`BNF` (syntax) notations into full (syntax) configuration objects, so merging with other grammars can be easier, if needed.
37753808
[/DOC_MARKDOWN]**/
3776-
pre_process: preprocess_grammar,
3809+
pre_process: preprocess_and_parse_grammar,
37773810

37783811
// parse a grammar
37793812
/**[DOC_MARKDOWN]
37803813
* __Method__: `parse`
37813814
*
37823815
* ```javascript
3783-
* parsedgrammar = CodeMirrorGrammar.parse( grammar );
3816+
* parsed_grammar = CodeMirrorGrammar.parse( grammar );
37843817
* ```
37853818
*
37863819
* This is used internally by the `CodeMirrorGrammar` Class

build/codemirror_grammar.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editor-grammar

src/main.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
// codemirror supposed to be available
14-
var $CodeMirror$ = CodeMirror || { Pass : { toString: function(){return "CodeMirror.Pass";} } },
14+
var $CodeMirror$ = 'undefined' !== typeof CodeMirror ? CodeMirror : { Pass : { toString: function(){return "CodeMirror.Pass";} } },
1515
// used for autocompletion
1616
RE_W = /[\w$]/, by_score = function( a, b ) { return b.score-a.score }
1717
;
@@ -324,7 +324,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
324324
* __For node:__
325325
*
326326
* ```javascript
327-
* CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
327+
* CodeMirrorGrammar = require('build/codemirror_grammar.js');
328328
* ```
329329
*
330330
* __For browser:__
@@ -343,7 +343,7 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
343343
* __Method__: `clone`
344344
*
345345
* ```javascript
346-
* cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
346+
* cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
347347
* ```
348348
*
349349
* Clone (deep) a `grammar`
@@ -357,7 +357,7 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
357357
* __Method__: `extend`
358358
*
359359
* ```javascript
360-
* extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
360+
* extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
361361
* ```
362362
*
363363
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
@@ -371,21 +371,21 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
371371
* __Method__: `pre_process`
372372
*
373373
* ```javascript
374-
* CodeMirrorGrammar.pre_process( grammar );
374+
* pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
375375
* ```
376376
*
377377
* This is used internally by the `CodeMirrorGrammar` Class `parse` method
378-
* In order to pre-process, in-place, a `JSON grammar`
379-
* to transform any shorthand configurations to full object configurations and provide defaults.
378+
* In order to pre-process a `JSON grammar` (in-place) to transform any shorthand configurations to full object configurations and provide defaults.
379+
* It also parses `PEG`/`BNF` (syntax) notations into full (syntax) configuration objects, so merging with other grammars can be easier, if needed.
380380
[/DOC_MARKDOWN]**/
381-
pre_process: preprocess_grammar,
381+
pre_process: preprocess_and_parse_grammar,
382382

383383
// parse a grammar
384384
/**[DOC_MARKDOWN]
385385
* __Method__: `parse`
386386
*
387387
* ```javascript
388-
* parsedgrammar = CodeMirrorGrammar.parse( grammar );
388+
* parsed_grammar = CodeMirrorGrammar.parse( grammar );
389389
* ```
390390
*
391391
* This is used internally by the `CodeMirrorGrammar` Class

test/demo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function codemirror_grammar_demo(code, lang, grammar)
44
document.getElementById('grammar-version').innerHTML = CodeMirrorGrammar.VERSION;
55

66
// 2. parse the grammar into a Codemirror syntax-highlight mode
7-
var mode = CodeMirrorGrammar.getMode(grammar);
7+
var mode = CodeMirrorGrammar.getMode( grammar );
88

99
// 3. register the mode with Codemirror
1010
CodeMirror.defineMode(lang, mode);

test/test.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
var CodeMirrorGrammar = require("../build/codemirror_grammar.js").CodeMirrorGrammar;
1+
var CodeMirrorGrammar = require("../build/codemirror_grammar.js");
2+
var vm = require("vm"), fs = require("fs"), echo = console.log;
3+
4+
function require_js( path, context )
5+
{
6+
context = context || {};
7+
var data = fs.readFileSync( path );
8+
vm.runInNewContext(data, context, path);
9+
return context;
10+
}
11+
12+
var grammar = require_js('./grammars/json.js');
13+
14+
//echo( CodeMirrorGrammar.VERSION );
15+
//echo( grammar.xml_grammar );
16+
echo( JSON.stringify(CodeMirrorGrammar.pre_process( grammar.json_grammar ), null, 4) );
217

3-
console.log(CodeMirrorGrammar.VERSION);
418

5-
console.log("CodeMirrorGrammar.getMode: " + ((CodeMirrorGrammar.getMode) ? true : false));

0 commit comments

Comments
 (0)