Skip to content

Commit

Permalink
v. 3.0.0 (update editor-grammar)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed Mar 24, 2016
1 parent 3a70393 commit c4b6a04
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
74 changes: 47 additions & 27 deletions build/prism_grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,11 +1230,11 @@ function preprocess_grammar( grammar )
return grammar;
}

function generate_autocompletion( token, follows )
function generate_autocompletion( token, follows, hash )
{
follows = follows || [];
hash = hash || {}; follows = follows || [];
if ( !token || !token.length ) return follows;
var i, l, j, m, tok, tok2, toks, i0;
var i, l, j, m, tok, tok2, toks, i0, w;
for(i=0,l=token.length; i<l; i++)
{
tok = token[i];
Expand All @@ -1243,35 +1243,39 @@ function generate_autocompletion( token, follows )
{
if ( !!tok.autocompletions )
{
follows.push.apply( follows, tok.autocompletions );
for(j=0,m=tok.autocompletions.length; j<m; j++)
{
w = tok.autocompletions[j];
if ( !hash[HAS]('w_'+w.word) )
{
follows.push( w );
hash['w_'+w.word] = 1;
}
}
}
else if ( (T_STR === tok.token.ptype) && (T_STR&get_type(tok.token.pattern)) && (tok.token.pattern.length > 1) )
{
follows.push( {word:''+tok.token.pattern, meta:tok.name, ci:!!tok.ci} );
}
/*else if ( T_CHARLIST === tok.token.ptype )
{
follows.push.apply( follows, tok.token.pattern.split('') );
if ( !hash[HAS]('w_'+tok.token.pattern) )
{
follows.push( {word:''+tok.token.pattern, meta:tok.name, ci:!!tok.ci} );
hash['w_'+tok.token.pattern] = 1;
}
}
else if ( T_REGEX === tok.token.ptype )
{
follows.push( tok.token.pattern[0].source );
}*/
}
else if ( T_ALTERNATION === tok.type )
{
generate_autocompletion( tok.token, follows );
generate_autocompletion( tok.token, follows, hash );
}
else if ( T_SEQUENCE_OR_NGRAM & tok.type )
{
j = 0; m = tok.token.length;
do{
generate_autocompletion( [tok2 = tok.token[j++]], follows );
}while(j < m && (((T_REPEATED & tok2.type) && 0 === tok2.min) || T_ACTION === tok2.type));
generate_autocompletion( [tok2 = tok.token[j++]], follows, hash );
}while(j < m && (((T_REPEATED & tok2.type) && (1 > tok2.min)) || T_ACTION === tok2.type));
}
else if ( T_REPEATED & tok.type )
{
generate_autocompletion( [tok.token[0]], follows );
generate_autocompletion( [tok.token[0]], follows, hash );
}
}
return follows;
Expand Down Expand Up @@ -2977,8 +2981,8 @@ function State( unique, s )
self.bline = s.bline;
self.status = s.status;
self.stack = s.stack.slice();
self.block = s.block;
self.token = s.token;
self.block = s.block;
// keep extra state only if error handling is enabled
if ( self.status & ERRORS )
{
Expand All @@ -3003,8 +3007,8 @@ function State( unique, s )
self.bline = -1;
self.status = s || 0;
self.stack = [];
self.block = null;
self.token = null;
self.block = null;
// keep extra state only if error handling is enabled
if ( self.status & ERRORS )
{
Expand Down Expand Up @@ -3036,8 +3040,8 @@ function state_dispose( state )
state.bline = null;
state.status = null;
state.stack = null;
state.block = null;
state.token = null;
state.block = null;
state.queu = null;
state.symb = null;
state.ctx = null;
Expand Down Expand Up @@ -3197,8 +3201,7 @@ var Parser = Class({
}
state.$blank$ = state.bline+1 === state.line;
}
state.$actionerr$ = false;
//state.token = null;
state.$actionerr$ = false; state.token = null;
stack = state.stack; line = state.line; pos = stream.pos;
type = false; notfound = true; err = false; just_space = false;
block_in_progress = state.block ? state.block.name : undef;
Expand Down Expand Up @@ -3231,7 +3234,7 @@ var Parser = Class({
{
tokenizer = interleaved_tokens[ii];
type = tokenize( tokenizer, stream, state, token );
if ( false !== type ) { notfound = false; state.token=tokenizer; break; }
if ( false !== type ) { notfound = false; break; }
}
if ( !notfound ) break;
}
Expand All @@ -3248,11 +3251,12 @@ var Parser = Class({
if ( tokenizer.status & REQUIRED_OR_ERROR )
{
// empty the stack of the syntax rule group of this tokenizer
state.token = tokenizer;
empty( stack, tokenizer.$id /*|| true*/ );
// skip this
if ( !stream.nxt( true ) ) { stream.spc( ); just_space = true; }
// generate error
err = true; notfound = false; state.token=tokenizer; break;
err = true; notfound = false; break;
}
// optional
/*else
Expand Down Expand Up @@ -3290,7 +3294,7 @@ var Parser = Class({
}
}
// not empty
if ( true !== type ) { notfound = false; state.token=tokenizer; break; }
if ( true !== type ) { notfound = false; break; }
}
}
}
Expand Down Expand Up @@ -3324,8 +3328,24 @@ var Parser = Class({
return T;
}

,autocompletion: function( token, follows ) {
return generate_autocompletion( token, follows||[] );
,autocompletion: function( state ) {
var stack = state.stack, i, token, type,
hash = {}, follows = generate_autocompletion( [ state.token ], [], hash );
for(i=stack.length-1; i>=0; i--)
{
token = stack[ i ]; type = token.type;
if ( T_REPEATED & type )
{
follows = generate_autocompletion( [ token ], follows, hash );
if ( (0 < token.min) && follows.length ) break;
}
else if ( (T_SIMPLE === type) || (T_ALTERNATION === type) || (T_SEQUENCE_OR_NGRAM & type) )
{
follows = generate_autocompletion( [ token ], follows, hash );
if ( follows.length ) break;
}
}
return follows;
}

,tokenize: function( stream, state, row ) {
Expand Down
Loading

0 comments on commit c4b6a04

Please sign in to comment.