javascript - I have a parser to make, i'd like to learn proper parsing -
i want parse text javascript. syntax want parse markup language. language has 2 main kind of markup:
$f56 mean following characters of color #f56. until following $ 3 hex char using color.
$i mean until following $z (closing tag) text in italic. other 1 letter tags.
so language composed of 3 character long hexa tags color , 1 letter long tags.
i can craft ugly parse text, storing char position , current status of tags (formatting , color) i'd learn proper parsing. give me few tips/principle make clean parser language ?
if know antlr might intrested in exploring http://www.antlr.org/wiki/display/antlr3/antlr3javascripttarget , generates javascript lexers , parsers
if set target language javascript
grammer t.g
grammar t; options { language=javascript; [other options] } ... ... parser.html
<script type="text/javascript" src="lib/antlr3-all-min.js"></script> <script type="text/javascript" src="tlexer.js"></script> <script type="text/javascript" src="tparser.js"></script> <script type="text/javascript" src="twalker.js"></script> <script type="text/javascript"> var input = "...what want feed parser...", cstream = new org.antlr.runtime.antlrstringstream(input), lexer = new tlexer(cstream), tstream = new org.antlr.runtime.commontokenstream(lexer), parser = new tparser(tstream), r = parser.entry_rule(); var nodes = new org.antlr.runtime.tree.commontreenodestream(r.gettree()); nodes.settokenstream(tstream); var walker = new twalker(nodes); walker.tree_entry_rule(); </script>
Comments
Post a Comment