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 181f264 commit 7b7746a
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 33 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
PrismGrammar = require('build/prism_grammar.js').PrismGrammar;
PrismGrammar = require('build/prism_grammar.js');
```

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

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

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

```javascript
extendedgrammar = PrismGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
extended_grammar = PrismGrammar.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
PrismGrammar.pre_process( grammar );
pre_processed_grammar = PrismGrammar.pre_process( grammar );
```

This is used internally by the `PrismGrammar` 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 = PrismGrammar.parse( grammar );
parsed_grammar = PrismGrammar.parse( grammar );
```

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

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

@
Expand Down
57 changes: 45 additions & 12 deletions build/prism_grammar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* PrismGrammar
* @version: 2.6.0
* @version: 2.6.1
*
* Transform a grammar specification in JSON format, into a syntax-highlighter for Prism
* https://github.com/foo123/prism-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 @@ -3172,7 +3205,7 @@ var Parser = Class({
/**
*
* PrismGrammar
* @version: 2.6.0
* @version: 2.6.1
*
* Transform a grammar specification in JSON format, into a syntax-highlighter for Prism
* https://github.com/foo123/prism-grammar
Expand Down Expand Up @@ -3337,7 +3370,7 @@ function get_mode( grammar, Prism )
* __For node:__
*
* ```javascript
* PrismGrammar = require('build/prism_grammar.js').PrismGrammar;
* PrismGrammar = require('build/prism_grammar.js');
* ```
*
* __For browser:__
Expand All @@ -3349,14 +3382,14 @@ function get_mode( grammar, Prism )
[/DOC_MARKDOWN]**/
var PrismGrammar = exports['PrismGrammar'] = {

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

// clone a grammar
/**[DOC_MARKDOWN]
* __Method__: `clone`
*
* ```javascript
* cloned = PrismGrammar.clone( grammar [, deep=true] );
* cloned_grammar = PrismGrammar.clone( grammar [, deep=true] );
* ```
*
* Clone (deep) a `grammar`
Expand All @@ -3370,7 +3403,7 @@ var PrismGrammar = exports['PrismGrammar'] = {
* __Method__: `extend`
*
* ```javascript
* extendedgrammar = PrismGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* extended_grammar = PrismGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* ```
*
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
Expand All @@ -3384,21 +3417,21 @@ var PrismGrammar = exports['PrismGrammar'] = {
* __Method__: `pre_process`
*
* ```javascript
* PrismGrammar.pre_process( grammar );
* pre_processed_grammar = PrismGrammar.pre_process( grammar );
* ```
*
* This is used internally by the `PrismGrammar` 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 = PrismGrammar.parse( grammar );
* parsed_grammar = PrismGrammar.parse( grammar );
* ```
*
* This is used internally by the `PrismGrammar` Class
Expand Down
6 changes: 3 additions & 3 deletions build/prism_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
16 changes: 8 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function get_mode( grammar, Prism )
* __For node:__
*
* ```javascript
* PrismGrammar = require('build/prism_grammar.js').PrismGrammar;
* PrismGrammar = require('build/prism_grammar.js');
* ```
*
* __For browser:__
Expand All @@ -185,7 +185,7 @@ var PrismGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `clone`
*
* ```javascript
* cloned = PrismGrammar.clone( grammar [, deep=true] );
* cloned_grammar = PrismGrammar.clone( grammar [, deep=true] );
* ```
*
* Clone (deep) a `grammar`
Expand All @@ -199,7 +199,7 @@ var PrismGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `extend`
*
* ```javascript
* extendedgrammar = PrismGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* extended_grammar = PrismGrammar.extend( grammar, basegrammar1 [, basegrammar2, ..] );
* ```
*
* Extend a `grammar` with `basegrammar1`, `basegrammar2`, etc..
Expand All @@ -213,21 +213,21 @@ var PrismGrammar = exports['@@MODULE_NAME@@'] = {
* __Method__: `pre_process`
*
* ```javascript
* PrismGrammar.pre_process( grammar );
* pre_processed_grammar = PrismGrammar.pre_process( grammar );
* ```
*
* This is used internally by the `PrismGrammar` 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 = PrismGrammar.parse( grammar );
* parsed_grammar = PrismGrammar.parse( grammar );
* ```
*
* This is used internally by the `PrismGrammar` Class
Expand Down
2 changes: 1 addition & 1 deletion test/demo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function prism_grammar_demo(code, lang, grammar, escapeHtml)
function prism_grammar_demo(code, lang, grammar)
{
document.getElementById('editor-version').innerHTML = '1.2.0';
document.getElementById('grammar-version').innerHTML = PrismGrammar.VERSION;
Expand Down
126 changes: 126 additions & 0 deletions test/grammar-json.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<!doctype html>
<html>
<head>

<meta charset="utf-8">

<link rel="stylesheet" href="prism/themes/prism-coy.css" data-noprefix />
<link rel="stylesheet" href="prism/plugins/line-numbers/prism-line-numbers.css" data-noprefix />
<script src="prism/prefixfree.min.js"></script>
<style type="text/css">
body {
position: relative;
}
pre {
max-height: 30em;
overflow: auto;
}
pre > code.highlight {
outline: .4em solid red;
outline-offset: .4em;
}
code[class*="language"] {
overflow: visible;
}
pre[class*="language"] {
overflow: scroll;
max-height: 50em;
}
</style>
<script src="prism/prism.js" data-manual ></script>
<script src="prism/plugins/line-numbers/prism-line-numbers.min.js"></script>
<script src="../build/prism_grammar.js"></script>
<script src="grammars/json.js"></script>
<script src="./demo.js"></script>

<title>Prism: Dynamic JSON Recursive Grammar Demo</title>
</head>
<body>

<h3>Prism (v.<span id="editor-version">0</span>) Grammar (v.<span id="grammar-version">0</span>): Dynamic JSON Recursive Grammar</h3>


<pre class="language-javascript line-numbers"><code id="code">
// 1. a complete json grammar in simple JSON format
{

// prefix ID for regular expressions used in the grammar
"RegExpID" : "RE::",

"Extra" : {

"fold" : "brace"

},

// Style model
"Style" : {

"comment" : "comment"
,"atom" : "entity"
,"number" : "number"
,"string" : "string"
,"error" : ""

},

// Lexical model
"Lex" : {

"comment:comment" : {"interleave":true,"tokens":[["//", null],["/*", "*/"]]}
,"string:escaped-block" : ["\"", "\""]
,"atom" : {"autocomplete":true,"tokens":["true","false","null"]}
,"number" : [
// floats
"RE::/\\d*\\.\\d+(e[\\+\\-]?\\d+)?/",
"RE::/\\d+\\.\\d*/",
"RE::/\\.\\d+/",
// integers
// hex
"RE::/0x[0-9a-fA-F]+L?/",
// binary
"RE::/0b[01]+L?/",
// octal
"RE::/0o[0-7]+L?/",
// decimal
"RE::/[1-9]\\d*(e[\\+\\-]?\\d+)?L?/",
// just zero
"RE::/0(?![\\dx])/"
]
,"other" : "RE::/\\S+/"

,"ctx:action" : {"context":true}
,"\\ctx:action" : {"context":false}
,"unique:action" : {"unique":["prop","$1"],"msg":"Duplicate object property \"$0\"","in-context":true}
,"invalid_json:error" : "Invalid JSON"

},

// Syntax model (optional)
"Syntax" : {

"literal_object" : "'{' ctx (literal_property_value (',' literal_property_value)*)? '}' \\ctx"
,"literal_array" : "'[' (literal_value (',' literal_value)*)? ']'"
// grammar recursion here
,"literal_value" : "atom | string | number | literal_array | literal_object"
,"literal_property_value" : "string unique ':' literal_value"
,"json" : "literal_value | other.error invalid_json"

},

// what to parse and in what order
// allow comments in json ;)
"Parser" : [ "comment", [ "json" ] ]

}
</code></pre>

<p></p>

<script>
// <![CDATA[
prism_grammar_demo(document.getElementById('code'), "json", json_grammar);
// ]]>
</script>
</body>
</html>
Loading

0 comments on commit 7b7746a

Please sign in to comment.