public class SourceGrammar extends Grammar
A grammar is a list of rules. Each rule defines a non-terminal symbol as a sequence of zero or more symbols (terminal or nonterminal).
A grammar can be used to create a parser for that grammar.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
logcategory |
Constructor and Description |
---|
SourceGrammar()
Create a new grammar.
|
SourceGrammar(ParserOptions options)
Create a new grammar with a specific set of options.
|
SourceGrammar(SourceGrammar current)
Create a new grammar from an existing grammar.
|
Modifier and Type | Method and Description |
---|---|
void |
addRule(NonterminalSymbol nonterminal,
java.util.List<Symbol> rhs)
Add a rule to the grammar.
|
void |
addRule(NonterminalSymbol nonterminal,
Symbol... rhs)
Add a rule to the grammar.
|
void |
addRule(Rule rule)
Add a rule to the grammar.
|
boolean |
contains(Rule candidate)
Does this grammar contain an equivalent rule?
|
ParserGrammar |
getCompiledGrammar(NonterminalSymbol seed) |
ParserGrammar |
getCompiledGrammar(ParserType parserType,
NonterminalSymbol seed) |
HygieneReport |
getHygieneReport(NonterminalSymbol seed)
Get a hygiene report for this grammar.
|
NonterminalSymbol |
getNonterminal(java.lang.String name)
Return the nonterminal symbol identified by name.
|
NonterminalSymbol |
getNonterminal(java.lang.String name,
java.util.List<ParserAttribute> attributes)
Return the nonterminal symbol identified by name.
|
NonterminalSymbol |
getNonterminal(java.lang.String name,
ParserAttribute attribute)
Return the nonterminal symbol identified by name.
|
GearleyParser |
getParser(ParserOptions options,
NonterminalSymbol seed)
Get a parser for this grammar.
|
GearleyParser |
getParser(ParserOptions options,
java.lang.String seed) |
ParserOptions |
getParserOptions()
Gets the parser options.
|
boolean |
isNullable(Symbol symbol)
Is the symbol nullable?
|
SourceGrammar |
resolveDuplicates()
Resolve duplicate symbols in the grammar.
|
void |
setMetadataProperty(java.lang.String name,
java.lang.String value)
Sets a metadata property.
|
getMetadataProperies, getMetadataProperty, getRules, getRulesBySymbol, getRulesForSymbol, getSymbols
public static final java.lang.String logcategory
public SourceGrammar()
public SourceGrammar(ParserOptions options)
options
- The options.public SourceGrammar(SourceGrammar current)
This is a convenient way to extend a previously closed grammar.
current
- the grammar to copypublic NonterminalSymbol getNonterminal(java.lang.String name)
Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
name
- The name of this symbol.java.lang.NullPointerException
- if the name is null.public NonterminalSymbol getNonterminal(java.lang.String name, ParserAttribute attribute)
Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
name
- The name of this symbol.attribute
- an attributejava.lang.NullPointerException
- if the name is null or the attribute is nullpublic NonterminalSymbol getNonterminal(java.lang.String name, java.util.List<ParserAttribute> attributes)
Nonterminal symbols are uniquely identified by their name.
Any string can be used as a name.
name
- The name of this symbol.attributes
- attributes to associate with this symbol, may be nulljava.lang.NullPointerException
- if the name is null.GrammarException
- if the symbol already exists and attributes are differentpublic void addRule(Rule rule)
Multiple rules can exist for the same NonterminalSymbol
. There must be at least
one rule for every nonterminal symbol that occurs on the "right hand side" of a rule.
Once added, a rule can never be removed.
rule
- The rule to addGrammarException
- if any nonterminal in the rule is not from this grammar, or if the grammar is closedpublic void addRule(NonterminalSymbol nonterminal, Symbol... rhs)
This is a convenience method that will construct the Rule
for you.
Multiple rules can exist for the same NonterminalSymbol
. There must be at least
one rule for every nonterminal symbol that occurs on the "right hand side" of a rule.
nonterminal
- The nonterminal symbol defined by this rule.rhs
- The list of symbols that define itGrammarException
- if any nonterminal in the rule is not from this grammar or if the grammar is closedpublic void addRule(NonterminalSymbol nonterminal, java.util.List<Symbol> rhs)
This is a convenience method that will construct the Rule
for you.
Multiple rules can exist for the same NonterminalSymbol
. There must be at least
one rule for every nonterminal symbol that occurs on the "right hand side" of a rule.
nonterminal
- The nonterminal symbol defined by this rule.rhs
- The list of symbols that define it.GrammarException
- if any nonterminal in the rule is not from this grammar or if the grammar is closedpublic boolean isNullable(Symbol symbol)
Grammar
A TerminalSymbol
is never nullable.
For a ParserGrammar
, the answer is definitive. For an SourceGrammar
,
a symbol that isn't currently nullable could become nullable by the addition of more rules.
isNullable
in class Grammar
symbol
- The symbol.public GearleyParser getParser(ParserOptions options, java.lang.String seed)
public ParserGrammar getCompiledGrammar(NonterminalSymbol seed)
public ParserGrammar getCompiledGrammar(ParserType parserType, NonterminalSymbol seed)
public GearleyParser getParser(ParserOptions options, NonterminalSymbol seed)
Returns a parser that will parse an input against the rules that define this grammar.
options
- the options for this parser.seed
- The NonterminalSymbol
that your input is expected to match.public SourceGrammar resolveDuplicates()
This method may return a different SourceGrammar. In this new grammar, all of the unique nonterminals will have unique names. (That is, if a nonterminal A occurs twice in the grammar with different parser attributes, it will have two different names in the resolved grammar.)
This method is public only so that it is possible to know what all of the nonterminals in the parse grammar will be.
public ParserOptions getParserOptions()
public HygieneReport getHygieneReport(NonterminalSymbol seed)
seed
- The seed rule for hygiene checking.public boolean contains(Rule candidate)
Two rules are equivalent if they have the same symbol, the same list of right-hand-side symbols, and if the attributes and optionality of every symbol on the right-hand-side are the same in both rules.
candidate
- the candidate rulepublic void setMetadataProperty(java.lang.String name, java.lang.String value)
Metadata properties exist solely for annotations by an application. They have no bearing on the function of the grammar.
name
- the name of the propertyvalue
- the value of the property, or null to remove a propertyjava.lang.NullPointerException
- if the name is null