Tuesday, April 13, 2010

Using XppScanner for lexical code parsing

The XppScanner class allows to parse X++ code in the way compiler does it. The simplest way to use it is to traverse all symbols in the given source code using iteration methods – firstSymbol() and nextSymbol(). The integer values returned by these methods are enumerated in the TokenTypes macro. For example, to check if there is “ttsbegin” statement in the code the following job can be used:

public static boolean checkTTSBegin(str _source)
{
    #TokenTypes

    XppScanner xppScanner = new XppScanner(_source);
    int symbol;

    for (symbol = xppScanner.firstSymbol(); symbol; symbol = xppScanner.nextSymbol())
    {
        if (symbol == #TTSBEGIN_SYM)
        {
            return true;
        }
    }

    return false;
}

PS. For this particular task you can, of course, use XppScanner.find() method.

2 comments: