Skip to content

The parser framework

Ioan CHIRIAC edited this page Feb 7, 2016 · 2 revisions

This parser looks a token ahead, and each function MUST consume his token.

NOTE : If you do not consume any token, then it will result in an infinite loop (maybe should handle this with a verification?) and if you don't consume the right tokens, it will result in a parse error.

Main functions

A parsing example :

    /**
     * reading an interface
     * <ebnf>
     * interface ::= class_scope? T_INTERFACE T_STRING (T_EXTENDS (NAMESPACE_NAME ',')* NAMESPACE_NAME)? '{' INTERFACE_BODY '}'
     * </ebnf>
     */
    ,read_interface: function(flag) {
      var name = this.expect(tokens.T_INTERFACE)
        .next()
        .expect(tokens.T_STRING)
        .text()
      ;
      var propExtends = false;
      if (this.next().token == tokens.T_EXTENDS) {
        propExtends =  this.next().read_list(
          this.read_namespace_name,
          ','
        );
      }
      return [
        'interface'
        , name
        , flag
        , propExtends
        , this.expect('{').next().read_interface_body()
      ];
    }

expect

checks the current token and raise an error if not match

next

eat current token and goes to the next

text

reads token contents (when the token is a T_STRING for example)

read_list

reads a list of tokens separated with a token

Clone this wiki locally