Axaptapedia is now maintained by AgileCadence For more information please click here.

ParserClass class

From Axaptapedia
Jump to: navigation, search


The class is not available from Ax 4

ParserClass performs syntax parsing of X++. With this class you can examine the structure of code and detect the method declarations, assignments, and other syntax constructions.

There is also a ScannerClass class providing lexical parsing

It is recommended to view EBNF Grammar of X++ before reading this article.

Possible applications[edit]

  • refactoring browser
  • obfuscator
  • job for automate insertion of logger calls


The basic scenario of usage is the following:

  • subclass ParserClass
  • override event handler methods
  • create instance of your subclass, providing source for parse as the constuctor parameter
  • call one of parseXXX methods

After this the event handler methods are called in order of syntax rule detection.


Methods can be divided into seversl categories

Parse methods[edit]

Parse methods when parse methods are called ParserClass parses source provided as a parameter for constructor and calls event handler methods when in detects error or syntax rule match. If parse method fails, it returns -1 otherwise it return 0

There are three parse methods:

  • parseClass - parse class declaration
  • parseExpr - parse X++ expression
  • parseFunction - parse method (but not the 'new' contructor - this is a problem which has no solution now)

Event handler methods[edit]

Event handler methods can be overriden in subclass to process information about code structure.

Rule methods[edit]

Rule method are called ahen parser detects matching some grammar rule. Each rule method returns object and takes zero and more object arguments.

This is, for example, synapsys of the ttsstmt_begin method which is fired when parser find ttsbegin; in the source code:

 protected Object ttsstmt_begin(Object _ttsbegin_sym, Object _semicolon_sym) 

Meaning of object parameters is completely defined by the class implementor. Each parameter correspond to the part of rule and can be null or the return value of the previousply called rule methods.

For example this is rule for the assignment statement:

  protected Object asgstmt(Object _lval_fld, Object _assign, Object _if_expr)

it corresponds to the following rule

  asg_stmt ::= lval_fld assign if_expr

it means that "assignment is a lval (lval_fld) followed by assign (assign) symbol followed by expression (if_expr)". If wee look at EBNF for "assign" we can found the following:

 ASSIGN ::= =
 ASSIGN ::= +=
 ASSIGN ::= -=

It can be translated as "assign can be '=', '+=', '-='". This rule can be mapped to the following ParserClass method: <xpp>

 protected Object assign(Object _asg_sym)

</xpp> but ParserClass does not provide any method for '=', '+=', '-=' so it puts null to the _asg_sym parameter and the concrete symbol can be got from the parser stack instead: <xpp>

 Object assign(Object _p1)
     // get concrete assign symbol, wrap it to object and return it
     return new SysAnyType(;

</xpp> For illustration when look for the following code <xpp>

 ParserClass parser=new NewParserClass('void test(){a=b;}');

</xpp> parser goes through source, and find the '=' symbol, then it calls the 'assign' method, gose forward, detects assignment end and calls asgstmt method with _assign parameter value set to the previously called 'assign' function return value.

Error method[edit]
 protected void error(int _errorcode, str _token, int _line, int _col)

this method can be overriden to catch the situation when source contains an error

State getters[edit]

State getters are methods intended to provide information about state of the parses when handling event methods. Each state method has one parameter - internal parser stack index. Each stack index corresponds to parameter of the rule methods. For example for the rule <xpp> protected Object asgstmt(Object _lval_fld, Object _assign, Object _if_expr) </xpp> the last parameter will have index 0, the first parameter - -3 the seconde - -2


This methods return the identifier name by the stack index


Returns the line number of the rule part start by the stack index


Returns number of column by the stack index. For the second and further rows subtract 1 from the returning value to ret real column number



KOAN -- (COde ANalyzer) will be set of toolf for analyzing the structure of Axapta code, which sometimes as mysterious as zen koans.

The first tool is KOAN Syntax Explorer

Koan shot.png

It visualy displays structure of the axapta code parsed by ParserClass