public class Antlr4ToAstParser extends Object implements AstParser
Parses grammar text into AST.
The general strategy of the parser is to try to produce an AST even if errors are encountered during parsing. It is up to the client to decide whether and for what purposes the result AST is usable in the presence of errors. Diagnostic information is available in this class.
The parser is invoked by a call to the grammar()
method.
This method may be invoked only once and on subsequent call
throws IllegalStateException
Note that the parser uses a ResourceLocator
in order to retrieve the following resources :
The default locator is a FileSystemLocator
.
After parsing a grammar the parser provides the following diagnostic information:
getErrors()
as a map
from error types to boolean values.getWarnings()
as a map
from error types to boolean values.In the NioGram mode of the parser all encountered problems are considered errors. In the ANTLR mode the following problems are considered warnings rather than errors:
Modifier and Type | Class and Description |
---|---|
static class |
Antlr4ToAstParser.GrammarType |
static class |
Antlr4ToAstParser.Mode
Parser mode.
|
ErrorDispatcher.ErrorType
Constructor and Description |
---|
Antlr4ToAstParser(InputStream inputStream)
Creates a new parser.
|
Antlr4ToAstParser(InputStream inputStream,
ResourceLocator resourceLocator)
Creates a new parser.
|
Antlr4ToAstParser(String fileName)
Creates a new parser.
|
Antlr4ToAstParser(String fileName,
ResourceLocator resourceLocator)
Creates a new parser.
|
Modifier and Type | Method and Description |
---|---|
void |
addErrorListener(ErrorListener errorListener)
Adds a new error listener to the dispatcher.
|
List<String> |
getDelegateGrammars()
Retrieves the list of delegate grammars declared in
the parsed grammar.
|
Set<String> |
getDuplicateNonterminalNames()
Retrieves the (possibly empty) set of duplicate
nonterminals in the parsed grammar.
|
Set<String> |
getDuplicateTerminalNames()
Retrieves the (possibly empty) set of duplicate
terminals in the parsed grammar.
|
Set<ErrorListener> |
getErrorListeners()
Retrieves the set of all error listeners
|
Map<ErrorDispatcher.ErrorType,Boolean> |
getErrors() |
String |
getFileName()
Retrieves the file name of the grammar.
|
String |
getGrammarName()
Retrieves the name of the parsed grammar.
|
Antlr4ToAstParser.GrammarType |
getGrammarType()
Retrieves the type of the parsed grammar.
|
Antlr4ToAstParser.Mode |
getMode()
Retrieves the mode of the parser.
|
int |
getOptionK()
Retrieves the value of the option k as
specified in the grammar text or -1 if
no option k was specified.
|
List<Pair<String,String>> |
getOptions()
Retrieves the list of options declared in the grammar
|
ResourceLocator |
getResourceLocator()
Retrieves the resource locator used by the parser.
|
Set<String> |
getUnknownNonterminalNames()
Retrieves the (possibly empty) set of unknown
nonterminals in the parsed grammar.
|
Set<String> |
getUnknownTerminalNames()
Retrieves the (possibly empty) set of unknown
terminals in the parsed grammar.
|
Map<ErrorDispatcher.ErrorType,Boolean> |
getWarnings() |
Grammar |
grammar()
Invokes the parser.
|
boolean |
hasErrors() |
boolean |
hasWarnings() |
void |
notifyErrorListeners(ErrorDispatcher.ErrorType errorType,
int line,
int position,
String message)
Notifies all error listeners of the dispatcher of an error.
|
void |
registerError(ErrorDispatcher.ErrorType error)
Register that an error has occurred
|
void |
registerWarning(ErrorDispatcher.ErrorType error)
Registers that an warning has occurred
|
void |
removeErrorListener(ErrorListener errorListener)
Remove an error listener from the dispatcher.
|
void |
removeErrorListeners()
Removes all error listeners from the error source;
|
void |
setFileName(String fileName)
Sets the file name of the grammar
|
void |
setMode(Antlr4ToAstParser.Mode mode)
Sets the mode of the parser.
|
void |
setOptionK(int optionK)
Sets the value of the option k to be
set as both K and KL into the created grammar.
|
void |
setResourceLocator(ResourceLocator resourceLocator)
Sets the resource locator to be used by the parser.
|
String |
toDotString()
Generates a DOT language representation of the parse tree.
|
String |
toXmlString()
Generates an XML representation of the parse tree.
|
void |
warnErrorListeners(ErrorDispatcher.ErrorType errorType,
int line,
int position,
String message)
Notifies all error listeners of the dispatcher of an warning.
|
public Antlr4ToAstParser(String fileName, ResourceLocator resourceLocator) throws IOException
fileName
- the name of the grammar fileresourceLocator
- the resource locator to be used by the parserIOException
- if the parser fails to access the grammar filepublic Antlr4ToAstParser(String fileName) throws IOException
FileSystemLocator
.fileName
- the name of the grammar fileIOException
- if the parser fails to access the grammar filepublic Antlr4ToAstParser(InputStream inputStream, ResourceLocator resourceLocator) throws IOException
inputStream
- an input stream for the grammar textresourceLocator
- the resource locator to be used by the parserIOException
- if the parser fails to access the streampublic Antlr4ToAstParser(InputStream inputStream) throws IOException
FileSystemLocator
.inputStream
- an input stream for the grammar textIOException
- if the parser fails to access the grammar filepublic Antlr4ToAstParser.Mode getMode()
public void setMode(Antlr4ToAstParser.Mode mode)
mode
- the mode to be setpublic String getFileName()
getFileName
in interface ErrorDispatcher
public void setFileName(String fileName)
fileName
- the file namepublic ResourceLocator getResourceLocator()
public void setResourceLocator(ResourceLocator resourceLocator)
resourceLocator
- the resource locatorpublic Grammar grammar()
public Antlr4ToAstParser.GrammarType getGrammarType()
public String getGrammarName()
public List<Pair<String,String>> getOptions()
public int getOptionK()
public void setOptionK(int optionK)
optionK
- the value of kpublic List<String> getDelegateGrammars()
public boolean hasErrors()
public Map<ErrorDispatcher.ErrorType,Boolean> getErrors()
public boolean hasWarnings()
public Map<ErrorDispatcher.ErrorType,Boolean> getWarnings()
public Set<String> getUnknownTerminalNames()
public Set<String> getDuplicateTerminalNames()
public Set<String> getUnknownNonterminalNames()
public Set<String> getDuplicateNonterminalNames()
public String toXmlString()
public String toDotString()
public Set<ErrorListener> getErrorListeners()
getErrorListeners
in interface ErrorDispatcher
public void addErrorListener(ErrorListener errorListener)
addErrorListener
in interface ErrorDispatcher
errorListener
- the listener.public void removeErrorListener(ErrorListener errorListener)
removeErrorListener
in interface ErrorDispatcher
errorListener
- the error listenerpublic void removeErrorListeners()
removeErrorListeners
in interface ErrorDispatcher
public void notifyErrorListeners(ErrorDispatcher.ErrorType errorType, int line, int position, String message)
notifyErrorListeners
in interface ErrorDispatcher
errorType
- the type of the errorline
- the line where a problem was encounteredposition
- the position in linemessage
- the error messagepublic void warnErrorListeners(ErrorDispatcher.ErrorType errorType, int line, int position, String message)
warnErrorListeners
in interface ErrorDispatcher
errorType
- the type of the errorline
- the line where a problem was encounteredposition
- the position in linemessage
- the error messagepublic void registerError(ErrorDispatcher.ErrorType error)
registerError
in interface ErrorDispatcher
error
- the error typepublic void registerWarning(ErrorDispatcher.ErrorType error)
registerWarning
in interface ErrorDispatcher
error
- the warning typeCopyright © 2018. All rights reserved.