|
Java APG, Version 1.0 Author Lowell D. Thomas Copyright © Coast to Coast Research, Inc. 2011 |
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.coasttocoastresearch.apg.Parser
public class Parser
The 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 | |
|---|---|
class |
Parser.CallbackData
Provisioned by the Parser for rule and UDT callback functions. |
class |
Parser.Result
Defines the Parser's results. |
static class |
Parser.RuleCallback
The base class for all rule callback functions. |
static class |
Parser.UdtCallback
Base class for all User-Defined Terminals (UDTs). |
| Constructor Summary | |
|---|---|
Parser(Grammar grammar)
The Parser constructor. |
|
| Method Summary | |
|---|---|
void |
addAstRuleNode(int id,
int offset,
int length,
boolean match)
NOTE: This function should only be called from UdtCallback or RuleCallback callback functions. |
void |
addAstUdtNode(int id,
int offset,
int length,
boolean match)
NOTE: This function should only be called from UdtCallback or RuleCallback callback functions. |
Ast |
enableAst(boolean enable)
Enables or disables the generation of an Abstract Syntax Tree (AST). |
Statistics |
enableStatistics(boolean enable)
Enables or disables the generation of a parsing statistics. |
Trace |
enableTrace(boolean enable)
Enables or disables the generation of a Trace. |
int |
executeRule(int id,
int offset)
NOTE: This function should only be called from UdtCallback or RuleCallback callback functions. |
int |
executeUdt(int id,
int offset)
NOTE: This function should only be called from UdtCallback or RuleCallback callback functions. |
Parser.Result |
parse()
Call this function to parser the input string. |
void |
setInputString(char[] input)
Sets the input string to be parsed. |
void |
setInputString(java.lang.String input)
Sets the input string to be parsed. |
void |
setMyData(java.lang.Object data)
Called to provision the callback functions with a user-defined data class. |
void |
setRuleCallback(int id,
Parser.RuleCallback callback)
Sets the user-defined callback function to be called when processing the identified rule nodes. |
void |
setStartRule(int start)
Sets the start rule. |
void |
setUdtCallback(int id,
Parser.UdtCallback callback)
Sets the user-defined callback function to be called when processing the identified UDT nodes. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Parser(Grammar grammar)
grammar - an instance of a Generator-generated Grammar class.| Method Detail |
|---|
public void addAstRuleNode(int id,
int offset,
int length,
boolean match)
throws java.lang.IllegalArgumentException,
java.lang.Exception
Adds a rule node to the AST. May alter the grammar the AST is representing and should only be used with care.
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.
java.lang.IllegalArgumentException - thrown if the id is out of range or the
java.lang.Exception - thrown if the callback function returns an illegal value.
An illegal value would be a phrase length L >= n,
where n is 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.)
public void addAstUdtNode(int id,
int offset,
int length,
boolean match)
throws java.lang.IllegalArgumentException,
java.lang.Exception
Adds a UDT node to the AST. May alter the grammar the AST is representing and should only be used with care.
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.
java.lang.IllegalArgumentException - thrown if the id is out of range or the
java.lang.Exception - thrown if the callback function returns an illegal value.
An illegal value would be a phrase length L >= n,
where n is 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.)public Ast enableAst(boolean enable)
enable - if true, an AST will be generated, if false, not.
By default enable is false.
Astpublic Statistics enableStatistics(boolean enable)
enable - if true, statistics will be generated, if false, not.
By default enable is false.
Statisticspublic Trace enableTrace(boolean enable)
enable - if true, an trace will be generated, if false, not.
By default enable is false.
Trace
public int executeRule(int id,
int offset)
throws java.lang.IllegalArgumentException,
java.lang.Exception
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.
id - grammar id of the rule to parse.
Must be in the range 0 - (rule count-1).offset - offset of the first character in the input string of the phrase to match.
-1 otherwise.
Phrase lengths must in the range 0 - (n-1),
where n is the remaining string length.
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 length L >= n
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.)
public int executeUdt(int id,
int offset)
throws java.lang.Exception
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.
id - grammar id of the UDT to parse.
Must be in the range 0 - (UDT count-1).offset - offset of the first character in the input string of the phrase to match.
-1 otherwise.
Phrase lengths must in the range 0 - (n-1),
where n is the remaining string length.
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 length L >= n
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.)
public Parser.Result parse()
throws java.lang.Exception
setInputString
is required prior to calling this function.
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.)Parser.Resultpublic void setInputString(char[] input)
parse()
input - the input stringpublic void setInputString(java.lang.String input)
parse()
input - the input stringpublic void setMyData(java.lang.Object data)
null by default.
data - any object of the user's choice.
public void setRuleCallback(int id,
Parser.RuleCallback callback)
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.GeneratorGrammar.RuleNames
public void setStartRule(int start)
throws java.lang.IllegalArgumentException
start = 0
which identifies the first rule in the grammar definition.
start - the identifier for the desired start rule.
java.lang.IllegalArgumentException - thrown if the start identifier is out of range.GeneratorGrammar.RuleNames
public void setUdtCallback(int id,
Parser.UdtCallback callback)
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.GeneratorGrammar.UdtNames
|
Java APG, Version 1.0 Author Lowell D. Thomas Copyright © Coast to Coast Research, Inc. 2011 |
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||