Permalink
Browse files

Added highlighting example

  • Loading branch information...
1 parent 5da4ae1 commit d50956256813ca78583d0ca6cbcf81aed4dd349d @qfox committed Mar 25, 2012
Showing with 15 additions and 0 deletions.
  1. +15 −0 README
View
15 README
@@ -28,11 +28,26 @@ Tokens are regular objects with several properties. Amongst them are .tokposw an
The parser has two modes for parsing: simple and extended. Simple mode is mainly for just parsing and returning the streams and a simple parse tree. There's not so much meta information here and this mode is mainly built for speed. The other mode has everything required for Zeon to do its job. This mode is toggled by the instance property .ast, which is true by default :)
+
Non-factory example:
var input = "foo";
var tree = []; // this should probably be refactored away some day
var tokenizer = new Tokenizer(input); // dito
var parser = new ZeParser(input, tokenizer, tree);
parser.parse(); // returns tree..., should never throw errors
+parser.tokenizer.fixValues(); // makes sure all tokens have a .value property
+
+
+Highlighting example:
+var parser = ZeParser.createParser(textarea.value); // textarea.value:input
+parser.tokenizer.fixValues(); // makes sure all tokens have a .value property
+var wtree = parser.tokenizer.wtree; // all the tokens ("token stream", including whitespace)
+textarea.className = '';
+var tokenstrings = wtree.map(function(t){
+ if (t.name == 14) textarea.className = 'error';
+ return '<span class="t'+t.name+'">'+(t.name==13?'\u29e6':(t.name==14?'\u292c':t.value)).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</span>';
+});
+// the string that would contain highlighted code
+// tokenstrings.join('');

0 comments on commit d509562

Please sign in to comment.