Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 54 lines (36 sloc) 2.776 kB
7ea47a8 @qfox First commit!
authored
1 This is a JavaScript parser.
2 http://github.com/qfox/ZeParser
3 (c) Peter van der Zee
4 http://qfox.nl
6689909 Updated readme quickly
Peter van der Zee authored
5
d26f409 @qfox merge github crap fix
authored
6
7fc4240 @qfox Update README
authored
7 Benchmark
8 http://qfox.github.com/ZeParser/benchmark.html
9
10 The Tokenizer is used by the parser. The parser tells the tokenizer whether the next token may be a regular expression or not. Without the parser, the tokenizer will fail if regular expression literals are used in the input.
6689909 Updated readme quickly
Peter van der Zee authored
11
12 Usage:
13 ZeParser.parse(input);
14
15 Returns a "parse tree" which is a tree of an array of arrays with tokens (regular objects) as leafs. Meta information embedded as properties (of the arrays and the tokens).
16
17 ZeParser.createParser(input);
18
19 Returns a new ZeParser instance which has already parsed the input. Amongst others, the ZeParser instance will have the properties .tree, .wtree and .btree.
20
21 .tree is the parse tree mentioned above.
22 .wtree ("white" tree) is a regular array with all the tokens encountered (including whitespace, line terminators and comments)
23 .btree ("black" tree) is just like .wtree but without the whitespace, line terminators and comments. This is what the specification would call the "token stream".
24
25 I'm aware that the naming convention is a bit awkward. It's a tradeoff between short and descriptive. The streams are used quite often in the analysis.
26
27 Tokens are regular objects with several properties. Amongst them are .tokposw and .tokposw, they correspond with their own position in the .wtree and .btree.
28
29 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 :)
30
d509562 @qfox Added highlighting example
authored
31
6689909 Updated readme quickly
Peter van der Zee authored
32 Non-factory example:
33
34 var input = "foo";
35 var tree = []; // this should probably be refactored away some day
36 var tokenizer = new Tokenizer(input); // dito
37 var parser = new ZeParser(input, tokenizer, tree);
38 parser.parse(); // returns tree..., should never throw errors
d509562 @qfox Added highlighting example
authored
39 parser.tokenizer.fixValues(); // makes sure all tokens have a .value property
40
41
42 Highlighting example:
24d6a42 @qfox Testing whether I can still commit :)
authored
43
d509562 @qfox Added highlighting example
authored
44 var parser = ZeParser.createParser(textarea.value); // textarea.value:input
45 parser.tokenizer.fixValues(); // makes sure all tokens have a .value property
46 var wtree = parser.tokenizer.wtree; // all the tokens ("token stream", including whitespace)
47 textarea.className = '';
48 var tokenstrings = wtree.map(function(t){
49 if (t.name == 14) textarea.className = 'error';
50 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>';
51 });
52 // the string that would contain highlighted code
53 // tokenstrings.join('');
Something went wrong with that request. Please try again.