Skip to content

Navigation Menu

Sign up
Simon Krajewski edited this page Jun 8, 2013 · 5 revisions

Writing a Parser

Basic steps

  1. Create a Parser class, extending hxparse.Parser.
  2. Add a constructor which calls super(lexerStream), where lexerStream is an instance of hxparse.LexerStream.
  3. Define a set of parsing functions.

Parsing functions

Anywhere within a parser, the expression switch stream is recognized as a stream matching. Its cases accept two kinds of expressions:

  1. Match a list of lexer tokens: case [Token1, Token2]:
  2. Match anything: case _:

Within the lexer token list, calls to other parsing functions can be made: case [e = parseExpr(), i = parseIdent()]:. The variables e and i are then available in the case body.

Parsing rules

Upon a switch stream expression, the current token is obtained by calling this.peek(0) and matched against the case list. If no match can be made, a non-critical hxparse.NoMatch exception is thrown: Because no token has been consumed, the stream is still intact matching can continue elsewhere.

As soon as a match can be made, the current token is consumed by calling this.junk(). If matching fails after this point, the stream is in an irrecoverable state and an hxparse.Unexpected exception is thrown.

Examples

A small parser for printf placeholders: https://github.com/Simn/hxparse/blob/master/test/PrintfParser.hx A parser for the haxe language: https://github.com/Simn/hxparse/blob/master/test/HaxeParser.hx

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /