Package apg
Class Parser
- java.lang.Object
-
- apg.Parser
-
public class Parser extends java.lang.ObjectThe Parser class is used to construct a parser for a specific grammar. It is initialized with a generated Grammar class object and optional UDT and rule callback functions. Optionally, the Parser class can be configured to generate an AST, parsing statistics, and/or a trace of the syntax tree nodes processed during the parse.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classParser.CallbackDataProvisioned by the Parser for rule and UDT callback functions.classParser.ResultDefines the Parser's results.static classParser.RuleCallbackThe base class for all rule callback functions.static classParser.UdtCallbackBase class for all User-Defined Terminals (UDTs).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAstRuleNode(int id, int offset, int length, boolean match)NOTE: This function should only be called from UdtCallback or RuleCallback callback functions.voidaddAstUdtNode(int id, int offset, int length, boolean match)NOTE: This function should only be called from UdtCallback or RuleCallback callback functions.AstenableAst(boolean enable)Enables or disables the generation of an Abstract Syntax Tree (AST).StatisticsenableStatistics(boolean enable)Enables or disables the generation of a parsing statistics.TraceenableTrace(boolean enable)Enables or disables the generation of a Trace.intexecuteRule(int id, int offset)NOTE: This function should only be called from UdtCallback or RuleCallback callback functions.intexecuteUdt(int id, int offset)NOTE: This function should only be called from UdtCallback or RuleCallback callback functions.Parser.Resultparse()Call this function to parser the input string.voidsetInputString(char[] input)Sets the input string to be parsed.voidsetInputString(java.lang.String input)Sets the input string to be parsed.voidsetMyData(java.lang.Object data)Called to provision the callback functions with a user-defined data class.voidsetRuleCallback(int id, Parser.RuleCallback callback)Sets the user-defined callback function to be called when processing the identified rule nodes.voidsetStartRule(int start)Sets the start rule.voidsetUdtCallback(int id, Parser.UdtCallback callback)Sets the user-defined callback function to be called when processing the identified UDT nodes.
-
-
-
Constructor Detail
-
Parser
public Parser(Grammar grammar)
The Parser constructor.- Parameters:
grammar- an instance of a Generator-generated Grammar class.
-
-
Method Detail
-
enableAst
public Ast enableAst(boolean enable)
Enables or disables the generation of an Abstract Syntax Tree (AST). The Ast class has no public constructor and this function is the only way to generate an instance.- Parameters:
enable- if true, an AST will be generated, if false, not. By default enable is false.- Returns:
- an instance of the Ast class.
- See Also:
Ast
-
enableTrace
public Trace enableTrace(boolean enable)
Enables or disables the generation of a Trace. Trace has no public constructor and this function is the only way to generate an instance.- Parameters:
enable- if true, an trace will be generated, if false, not. By default enable is false.- Returns:
- an instance of the Trace class.
- See Also:
Trace
-
enableStatistics
public Statistics enableStatistics(boolean enable)
Enables or disables the generation of a parsing statistics. Statistics has no public constructor and this function is the only way to generate an instance.- Parameters:
enable- if true, statistics will be generated, if false, not. By default enable is false.- Returns:
- an instance of the Statistics class.
- See Also:
Statistics
-
setStartRule
public void setStartRule(int start) throws java.lang.IllegalArgumentExceptionSets the start rule. By defaultstart = 0which identifies the first rule in the grammar definition.- Parameters:
start- the identifier for the desired start rule.- Throws:
java.lang.IllegalArgumentException- thrown if thestartidentifier is out of range.- See Also:
GeneratorGrammar.RuleNames
-
setInputString
public void setInputString(java.lang.String input)
Sets the input string to be parsed. A call to this function is required prior to callingparse()- Parameters:
input- the input string
-
setInputString
public void setInputString(char[] input)
Sets the input string to be parsed. A call to this function is required prior to callingparse()- Parameters:
input- the input string
-
setRuleCallback
public void setRuleCallback(int id, Parser.RuleCallback callback)Sets the user-defined callback function to be called when processing the identified rule nodes.- Parameters:
id- identifier of the rule for which this callback is to be called.callback- an instance of a user-written extension of the RuleCallback class.- See Also:
GeneratorGrammar.RuleNames
-
setUdtCallback
public void setUdtCallback(int id, Parser.UdtCallback callback)Sets the user-defined callback function to be called when processing the identified UDT nodes.- Parameters:
id- identifier of the UDT for which this callback is to be called.callback- an instance of a user-written extension of the UdtCallback class.- See Also:
GeneratorGrammar.UdtNames
-
setMyData
public void setMyData(java.lang.Object data)
Called to provision the callback functions with a user-defined data class.nullby default.- Parameters:
data- any object of the user's choice.
-
parse
public Parser.Result parse() throws java.lang.Exception
Call this function to parser the input string. A call tosetInputStringis required prior to calling this function.- Returns:
- a Result class with information about the parser results.
- Throws:
java.lang.Exception- thrown if any callback function returns an illegal value. Illegal values are phrase lengths longer than the length of the remaining input string or a zero (empty) length by a UDT designated as non-empty. (UDTs having names beginning with "u_" are designated as non-empty. UDTs having names beginning with "e_" are designated as empty.)- See Also:
Parser.Result
-
executeRule
public int executeRule(int id, int offset) throws java.lang.IllegalArgumentException, java.lang.ExceptionNOTE: This function should only be called from UdtCallback or RuleCallback callback functions. Results will be unpredictable if called otherwise.Parses the requested rule as if it were a child node of the calling function. A call to this function alters the grammar being parsed and should be used with care.
- Parameters:
id- grammar id of the rule to parse. Must be in the range0 - (rule count-1).offset- offset of the first character in the input string of the phrase to match.- Returns:
- the length of the matched phrase if the parse is successful.
-1otherwise. Phrase lengths must in the range0 - (n-1), wherenis the remaining string length. - Throws:
java.lang.IllegalArgumentException- thrown if id is out of range (see above).java.lang.Exception- thrown if the callback function returns an illegal value. An illegal value would be a phrase lengthL >= nor a zero (empty) length by a UDT designated as non-empty. (UDTs having names beginning with "u_" are designated as non-empty. UDTs having names beginning with "e_" are designated as empty.)
-
executeUdt
public int executeUdt(int id, int offset) throws java.lang.ExceptionNOTE: This function should only be called from UdtCallback or RuleCallback callback functions. Results will be unpredictable if called otherwise.Parses the requested UDT as if it were a child node of the calling function. A call to this function alters the grammar being parsed and should be used with care.
- Parameters:
id- grammar id of the UDT to parse. Must be in the range0 - (UDT count-1).offset- offset of the first character in the input string of the phrase to match.- Returns:
- the length of the matched phrase if the parse is successful.
-1otherwise. Phrase lengths must in the range0 - (n-1), wherenis the remaining string length. - Throws:
java.lang.IllegalArgumentException- thrown if id is out of range (see above).java.lang.Exception- thrown if the callback function returns an illegal value. An illegal value would be a phrase lengthL >= nor a zero (empty) length by a UDT designated as non-empty. (UDTs having names beginning with "u_" are designated as non-empty. UDTs having names beginning with "e_" are designated as empty.)
-
addAstRuleNode
public void addAstRuleNode(int id, int offset, int length, boolean match) throws java.lang.IllegalArgumentException, java.lang.ExceptionNOTE: This function should only be called from UdtCallback or RuleCallback callback functions. Results will be unpredictable if called otherwise.Adds a rule node to the AST. May alter the grammar the AST is representing and should only be used with care.
- Parameters:
id- grammar id of the rule name node to add.offset- offset into the input string to the first character of the phrase this node has matched.length- length of the matched phrase. Must not be larger than the remaining input string length.match- is not used.- Throws:
java.lang.IllegalArgumentException- thrown if the id is out of range or thejava.lang.Exception- thrown if the callback function returns an illegal value. An illegal value would be a phrase lengthL >= n, wherenis the remaining length of the input string, or a zero (empty) length by a UDT designated as non-empty. (UDTs having names beginning with "u_" are designated as non-empty. UDTs having names beginning with "e_" are designated as empty.)
-
addAstUdtNode
public void addAstUdtNode(int id, int offset, int length, boolean match) throws java.lang.IllegalArgumentException, java.lang.ExceptionNOTE: This function should only be called from UdtCallback or RuleCallback callback functions. Results will be unpredictable if called otherwise.Adds a UDT node to the AST. May alter the grammar the AST is representing and should only be used with care.
- Parameters:
id- grammar id of the UDT name node to add.offset- offset into the input string to the first character of the phrase this node has matched.length- length of the matched phrase. Must not be larger than the remaining input string length.match- is not used.- Throws:
java.lang.IllegalArgumentException- thrown if the id is out of range or thejava.lang.Exception- thrown if the callback function returns an illegal value. An illegal value would be a phrase lengthL >= n, wherenis the remaining length of the input string, or a zero (empty) length by a UDT designated as non-empty. (UDTs having names beginning with "u_" are designated as non-empty. UDTs having names beginning with "e_" are designated as empty.)
-
-