Skip to content

Commit

Permalink
v.2.6.1
Browse files Browse the repository at this point in the history
parse PEG/BNF in pre_proces method as well
fix nodejs loading
  • Loading branch information
Nikos M committed Oct 30, 2015
1 parent 7bf3093 commit 3fea5c6
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 38 deletions.
14 changes: 7 additions & 7 deletions api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__For node:__

```javascript
CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
CodeMirrorGrammar = require('build/codemirror_grammar.js');
```

__For browser:__
Expand All @@ -20,7 +20,7 @@ __For browser:__
__Method__: `clone`

```javascript
cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
```

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

```javascript
extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
```

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

```javascript
CodeMirrorGrammar.pre_process( grammar );
pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
```

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



__Method__: `parse`

```javascript
parsedgrammar = CodeMirrorGrammar.parse( grammar );
parsed_grammar = CodeMirrorGrammar.parse( grammar );
```

This is used internally by the `CodeMirrorGrammar` Class
Expand Down
2 changes: 1 addition & 1 deletion beeld.config
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tasks =[{}]

"@@ROOT@@" = "this"
"@@EXPORTS@@" = "exports"
"@@VERSION@@" = "2.6.0"
"@@VERSION@@" = "2.6.1"
"@@MODULE_NAME@@" = "CodeMirrorGrammar"

@
Expand Down
59 changes: 46 additions & 13 deletions build/codemirror_grammar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* CodeMirrorGrammar
* @version: 2.6.0
* @version: 2.6.1
*
* Transform a grammar specification in JSON format, into a syntax-highlight parser mode for CodeMirror
* https://github.com/foo123/codemirror-grammar
Expand Down Expand Up @@ -36,7 +36,7 @@ else if ( !(name in root) )
"use strict";
/**
* EditorGrammar Codebase
* @version: 2.6.0
* @version: 2.6.1
*
* https://github.com/foo123/editor-grammar
**/
Expand Down Expand Up @@ -1934,6 +1934,39 @@ function get_block_types( grammar, the_styles )
return blocks;
}

function preprocess_and_parse_grammar( grammar )
{
var processed = {}; // for recursive references
grammar.Lex = grammar.Lex || {}; grammar.Syntax = grammar.Syntax || {};
grammar = preprocess_grammar( grammar );
if ( grammar.Parser && grammar.Parser.length )
{
iterate( function process( i, T ) {
var id = T[ i ], t, token, type, tokens;
if ( processed[id] ) return;
if ( T_ARRAY & get_type( id ) )
{
// literal n-gram as array
t = id; id = "NGRAM_" + t.join("_");
if ( !grammar.Syntax[ id ] ) grammar.Syntax[ id ] = {type:"ngram", tokens:t};
}
token = get_backreference( id, grammar.Lex, grammar.Syntax );
if ( T_STR & get_type( token ) )
{
token = parse_peg_bnf_notation( token, grammar.Lex, grammar.Syntax );
token = grammar.Lex[ token ] || grammar.Syntax[ token ] || null;
}
if ( token )
{
processed[id] = token;
type = token.type ? tokenTypes[ token.type[LOWER]( ).replace( dashes_re, '' ) ] || T_SIMPLE : T_SIMPLE;
if ( T_COMPOSITE & type ) iterate( process, 0, token.tokens.length-1, token.tokens );
}
}, 0, grammar.Parser.length-1, grammar.Parser );
}
return grammar;
}

function parse_grammar( grammar )
{
var RegExpID, tokens,
Expand Down Expand Up @@ -3396,7 +3429,7 @@ var Folder = {
/**
*
* CodeMirrorGrammar
* @version: 2.6.0
* @version: 2.6.1
*
* Transform a grammar specification in JSON format, into a syntax-highlight parser mode for CodeMirror
* https://github.com/foo123/codemirror-grammar
Expand All @@ -3406,7 +3439,7 @@ var Folder = {


// codemirror supposed to be available
var $CodeMirror$ = CodeMirror || { Pass : { toString: function(){return "CodeMirror.Pass";} } },
var $CodeMirror$ = 'undefined' !== typeof CodeMirror ? CodeMirror : { Pass : { toString: function(){return "CodeMirror.Pass";} } },
// used for autocompletion
RE_W = /[\w$]/, by_score = function( a, b ) { return b.score-a.score }
;
Expand Down Expand Up @@ -3719,7 +3752,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
* __For node:__
*
* ```javascript
* CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
* CodeMirrorGrammar = require('build/codemirror_grammar.js');
* ```
*
* __For browser:__
Expand All @@ -3731,14 +3764,14 @@ function get_mode( grammar, DEFAULT, CodeMirror )
[/DOC_MARKDOWN]**/
var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {

VERSION: "2.6.0",
VERSION: "2.6.1",

// clone a grammar
/**[DOC_MARKDOWN]
* __Method__: `clone`
*
* ```javascript
* cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
* cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
* ```
*
* Clone (deep) a `grammar`
Expand All @@ -3752,7 +3785,7 @@ var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {
* __Method__: `extend`
*
* ```javascript
* extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* ```
*
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
Expand All @@ -3766,21 +3799,21 @@ var CodeMirrorGrammar = exports['CodeMirrorGrammar'] = {
* __Method__: `pre_process`
*
* ```javascript
* CodeMirrorGrammar.pre_process( grammar );
* pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
* ```
*
* This is used internally by the `CodeMirrorGrammar` Class `parse` method
* In order to pre-process, in-place, a `JSON grammar`
* to transform any shorthand configurations to full object configurations and provide defaults.
* In order to pre-process a `JSON grammar` (in-place) to transform any shorthand configurations to full object configurations and provide defaults.
* It also parses `PEG`/`BNF` (syntax) notations into full (syntax) configuration objects, so merging with other grammars can be easier, if needed.
[/DOC_MARKDOWN]**/
pre_process: preprocess_grammar,
pre_process: preprocess_and_parse_grammar,

// parse a grammar
/**[DOC_MARKDOWN]
* __Method__: `parse`
*
* ```javascript
* parsedgrammar = CodeMirrorGrammar.parse( grammar );
* parsed_grammar = CodeMirrorGrammar.parse( grammar );
* ```
*
* This is used internally by the `CodeMirrorGrammar` Class
Expand Down
6 changes: 3 additions & 3 deletions build/codemirror_grammar.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion editor-grammar
Submodule editor-grammar updated 1 files
+33 −0 src/helpers.js
18 changes: 9 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


// codemirror supposed to be available
var $CodeMirror$ = CodeMirror || { Pass : { toString: function(){return "CodeMirror.Pass";} } },
var $CodeMirror$ = 'undefined' !== typeof CodeMirror ? CodeMirror : { Pass : { toString: function(){return "CodeMirror.Pass";} } },
// used for autocompletion
RE_W = /[\w$]/, by_score = function( a, b ) { return b.score-a.score }
;
Expand Down Expand Up @@ -324,7 +324,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
* __For node:__
*
* ```javascript
* CodeMirrorGrammar = require('build/codemirror_grammar.js').CodeMirrorGrammar;
* CodeMirrorGrammar = require('build/codemirror_grammar.js');
* ```
*
* __For browser:__
Expand All @@ -343,7 +343,7 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `clone`
*
* ```javascript
* cloned = CodeMirrorGrammar.clone( grammar [, deep=true] );
* cloned_grammar = CodeMirrorGrammar.clone( grammar [, deep=true] );
* ```
*
* Clone (deep) a `grammar`
Expand All @@ -357,7 +357,7 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `extend`
*
* ```javascript
* extendedgrammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* extended_grammar = CodeMirrorGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* ```
*
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
Expand All @@ -371,21 +371,21 @@ var CodeMirrorGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `pre_process`
*
* ```javascript
* CodeMirrorGrammar.pre_process( grammar );
* pre_processed_grammar = CodeMirrorGrammar.pre_process( grammar );
* ```
*
* This is used internally by the `CodeMirrorGrammar` Class `parse` method
* In order to pre-process, in-place, a `JSON grammar`
* to transform any shorthand configurations to full object configurations and provide defaults.
* In order to pre-process a `JSON grammar` (in-place) to transform any shorthand configurations to full object configurations and provide defaults.
* It also parses `PEG`/`BNF` (syntax) notations into full (syntax) configuration objects, so merging with other grammars can be easier, if needed.
[/DOC_MARKDOWN]**/
pre_process: preprocess_grammar,
pre_process: preprocess_and_parse_grammar,

// parse a grammar
/**[DOC_MARKDOWN]
* __Method__: `parse`
*
* ```javascript
* parsedgrammar = CodeMirrorGrammar.parse( grammar );
* parsed_grammar = CodeMirrorGrammar.parse( grammar );
* ```
*
* This is used internally by the `CodeMirrorGrammar` Class
Expand Down
2 changes: 1 addition & 1 deletion test/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function codemirror_grammar_demo(code, lang, grammar)
document.getElementById('grammar-version').innerHTML = CodeMirrorGrammar.VERSION;

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

// 3. register the mode with Codemirror
CodeMirror.defineMode(lang, mode);
Expand Down
19 changes: 16 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
var CodeMirrorGrammar = require("../build/codemirror_grammar.js").CodeMirrorGrammar;
var CodeMirrorGrammar = require("../build/codemirror_grammar.js");
var vm = require("vm"), fs = require("fs"), echo = console.log;

function require_js( path, context )
{
context = context || {};
var data = fs.readFileSync( path );
vm.runInNewContext(data, context, path);
return context;
}

var grammar = require_js('./grammars/json.js');

//echo( CodeMirrorGrammar.VERSION );
//echo( grammar.xml_grammar );
echo( JSON.stringify(CodeMirrorGrammar.pre_process( grammar.json_grammar ), null, 4) );

console.log(CodeMirrorGrammar.VERSION);

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

0 comments on commit 3fea5c6

Please sign in to comment.