|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.vishia.zbnf.ZbnfParser
public class ZbnfParser
An instance of ZbnfParser contains a syntax prescript inside and is able to parse a text, test the syntax and output
a tree of information given in the input text.
The invocation is in followed manner:
ZbnfParser parser = new ZbnfParser(reportConsole);
try{ parser.setSyntax(syntaxString);}
catch(ParseException exception)
{ writeError("parser reading syntax error: " + exception.getMessage();
return;
}
if(!parser.parse(inputString))
{ writeError(parser.getSyntaxErrorReport());
}
else
{ ParseResultItem resultItem = parser.getFirstParseResult();
while( resultItem != null)
{ evaluateResult(resultItem);
resultItem = resultItem.next(null))
}
}
setSyntax(StringPart) is to be defined in the Semantic Backus Naur-Form
(ZBNF, Z is a reverse S for Semantic).
It is given as a String or StringPart.
The method setSyntax, reads the string and convert it in internal data. The input string (mostly readed from a file)
may be consist of a sequence of variables beginning with $ and syntax terms. A syntax term is described
on the class ZbnfSyntaxPrescript, because this class converts a syntax term in an internal tree of syntax nodes.
Downside it is shown an example of a syntax file or string with all variables.
<?ZBNF-www.vishia.org version="1.0" encoding="iso-8859-1" ?> ##this first line is not prescribed but possible.
$setLinemode. ##if set, than the newline char \n is not overwritten as whitespace
$endlineComment=##. ##defines the string introducing a comment to eol, default is //
$comment=[*...*]. ##... between [* ... *] all chars are ignored, default is /*...* /
$keywords=if|else. ##that identifiers are not accepted as identifiers parsing by <$?...>
$inputEncodingKeyword="encoding". ##it helps to define the encoding of the input file via a keyword input-file
$inputEncoding="UTF-8". ##it helps to define the encoding of the input file (useable outside parser core)
$xmlns:nskey="value". ##defines a namespace key for XML output (useable outside parser core)
component::=<$?name>=<#?number> { <value> , }. ##The first syntax term is the toplevel syntax.
value::= val = [ a | b | c]. ##another syntax term
setSkippingComment(String, String, boolean), setSkippingEndlineComment(String, boolean),
setWhiteSpaces(String), setLinemode(boolean) is not occured after setSyntax(String).<$NoWhiteSpaces>-construct,
see ZbnfSyntaxPrescript.
getFirstParseResult() to get the toplevel item.
| Nested Class Summary | |
|---|---|
private class |
ZbnfParser.PrescriptParser
Class to organize parsing of a component with a own prescript. |
| Field Summary | |
|---|---|
protected boolean |
bConstantSyntaxAsParseResult
Set if constant syntax (terminate morphems) also should stored. |
(package private) boolean |
bStoreComment
If it is true, the comment is stored in the ParserStore and is supplied by calling getFirstParseResult() and from there calling next(). |
(package private) boolean |
bStoreEndlineComment
If it is true, the end-line-comment is stored in the ParserStore and is supplied by calling getFirstParseResult() and from there calling next(). |
(package private) boolean |
bStoreNewline
If it is true, a newline is stored in the ParserStore and is supplied by calling getFirstParseResult() and from there calling next(). |
(package private) boolean |
bStoreOneSpaceOnWhitespaces
If it is true, one space is stored on whitespaces in the ParserStore and is supplied by calling getFirstParseResult() and from there calling next(). |
(package private) boolean |
bStoreWhiteSpaces
If it is true, the complete white spaces are stored in the ParserStore and is supplied by calling getFirstParseResult() and from there calling next(). |
private java.nio.charset.Charset |
charsetInput
|
protected int |
idReportComponentParsing
|
protected int |
idReportError
|
protected int |
idReportInfo
|
protected int |
idReportParsing
The ident to report the progress of parsing. |
(package private) java.util.TreeMap<java.lang.String,java.lang.String> |
listKeywords
Keywords |
(package private) java.util.ArrayList<ZbnfParseResultItem> |
listParseResultOnError
founded content on rightest parsing error position. |
protected java.util.TreeMap<java.lang.String,ZbnfSyntaxPrescript> |
listSubPrescript
The list of some sub syntax definitons. |
private ZbnfSyntaxPrescript |
mainScript
The main syntaxprescript setted from setSyntax(StringPart). |
private int |
maxParseResultEntriesOnError
Maximum number of shown parsing results on error. |
protected int |
nLevelReportComponentParsing
The used reportlevel for output the progress of parsing. |
protected int |
nLevelReportError
The used reportlevel for output the progress of parsing. |
protected int |
nLevelReportInfo
The used reportlevel for output the progress of parsing. |
protected int |
nLevelReportParsing
The used reportlevel for output the progress of parsing. |
protected int |
nReportLevel
The current report level. |
private ZbnfParserStore |
parserStoreTopLevel
The actual parse result buffer. |
protected long |
posRightestError
The string and position found on the rightest position of an parse fault. |
protected ZbnfParser.PrescriptParser |
prescriptParserTopLevel
|
protected Report |
report
To Report something. |
(package private) java.lang.String |
sCommentStringEnd
The end of a comment string, it shoult be set if sCommentStringStart is not null. |
(package private) java.lang.String |
sCommentStringStart
The start of a comment string, if null than no comment is known. |
private static java.lang.String |
sEmpty
Helpfull empty string to build some spaces in strings. |
(package private) java.lang.String |
sEndlineCommentStringStart
The start of a comment string, if null than no comment is known. |
protected java.lang.String |
sExpectedSyntax
Required syntax on rightest parsing error position |
protected java.lang.String |
sInputEncoding
If the syntax prescript contains $inputEncoding="...". |
protected java.lang.String |
sInputEncodingKeyword
If the syntax prescript contains $inputEncodingKeyword="...". |
protected java.lang.String |
sRightestError
The string and position found on the rightest position of an parse fault. |
protected ZbnfParser.PrescriptParser.SubParser |
subParserTopLevel
|
(package private) java.lang.String |
sWhiteSpaces
Chars there are detect as white spaces: |
(package private) java.util.TreeMap<java.lang.String,java.lang.String> |
xmlnsList
xmlns |
protected java.lang.String |
xxxsFoundedSyntax
founded syntax on rightest parsing error position |
| Constructor Summary | |
|---|---|
ZbnfParser(Report report)
Creates a empty parser instance. |
|
ZbnfParser(Report report,
int maxParseResultEntriesOnError)
Creates a empty parser instance. |
|
| Method Summary | |
|---|---|
java.lang.String |
getExpectedSyntaxOnError()
Returns the expected syntax on error position. |
ZbnfParseResultItem |
getFirstParseResult()
Returns the first parse result item to start stepping to the results. |
java.lang.String |
getFoundedInputOnError()
Returns about 50 chars of the input string founded at the parsing error position. |
java.nio.charset.Charset |
getInputEncoding()
Returns the setting of $inputEncoding="...". |
java.lang.String |
getInputEncodingKeyword()
Returns the setting of $inputEncodingKeyword="...". |
long |
getInputPositionOnError()
Returns the position of error in input string. |
java.lang.String |
getLastFoundedResultOnError()
Returns the up to now founded result on error position. |
java.lang.String |
getSyntaxErrorReport()
assembles a string with a user readable syntax error message. |
java.util.TreeMap<java.lang.String,java.lang.String> |
getXmlnsFromSyntaxPrescript()
Returns a TreeMap of all xmlns keys and strings. |
private void |
importScript(java.lang.String sFile,
java.lang.String sDirParent)
|
boolean |
parse(java.lang.String input)
Parsed a given Input and produces a parse result. |
boolean |
parse(StringPart input)
parses a given Input and produces a parse result. |
boolean |
parse(StringPart input,
java.util.List<java.lang.String> additionalInfo)
parses a given Input, see [ parse(StringPart), but write additional semantic informations
into the first parse result (into the top level component). |
void |
reportStore(Report report)
Reports the whole content of the parse result in the Report.fineInfo-level. |
void |
reportStore(Report report,
int reportLevel)
|
void |
reportStore(Report report,
int reportLevel,
java.lang.String sTitle)
Reports the whole content of the parse result. |
private int |
reportStoreComponent(ZbnfParseResultItem parseResultItem,
Report report,
int level,
ZbnfParseResultItem parent,
int reportLevel)
Inner method to report the content of the parse result |
void |
reportSyntax(Report report,
int reportLevel)
Reports the syntax. |
private ZbnfSyntaxPrescript |
searchSyntaxPrescript(java.lang.String sSyntax)
|
void |
setLinemode(boolean bTrue)
Sets the line mode or not. |
void |
setReportIdents(int identError,
int identInfo,
int identComponent,
int identFine)
sets the ident number for report of the progress of parsing. |
void |
setSkippingComment(java.lang.String sCommentStringStart,
java.lang.String sCommentStringEnd,
boolean bStoreComment)
Set the mode of skipping comments. |
void |
setSkippingEndlineComment(java.lang.String sCommentStringStart,
boolean bStoreComment)
Set the mode of skipping comments to end of line. |
boolean |
setStoringConstantSyntax(boolean bStore)
Determines wether or not constant syntax (teminal syntax items or terminal morphes) should also strored in the result buffer. |
void |
setSyntax(java.io.File fileSyntax)
|
protected void |
setSyntax(java.io.InputStream input)
|
void |
setSyntax(java.lang.String syntax)
Sets the syntax from given string. |
void |
setSyntax(StringPart syntax)
Sets the syntax from given String. |
private void |
setSyntax(StringPart syntax,
java.lang.String sDirParent)
|
void |
setWhiteSpaces(java.lang.String sWhiteSpaces)
Sets the chars which are recognized as white spaces. |
private void |
stop()
It's a debug helper. |
protected void |
throwSyntaxErrorException(java.lang.String text)
throws a ParseException with the infos of syntax error from last parsing. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private static final java.lang.String sEmpty
protected final Report report
protected int nReportLevel
protected int nLevelReportParsing
protected int nLevelReportComponentParsing
protected int nLevelReportInfo
protected int nLevelReportError
protected int idReportParsing
protected int idReportComponentParsing
protected int idReportInfo
protected int idReportError
protected final java.util.TreeMap<java.lang.String,ZbnfSyntaxPrescript> listSubPrescript
java.util.TreeMap<java.lang.String,java.lang.String> listKeywords
java.util.TreeMap<java.lang.String,java.lang.String> xmlnsList
protected boolean bConstantSyntaxAsParseResult
private ZbnfSyntaxPrescript mainScript
setSyntax(StringPart).
protected ZbnfParser.PrescriptParser prescriptParserTopLevel
protected ZbnfParser.PrescriptParser.SubParser subParserTopLevel
protected java.lang.String sRightestError
protected java.lang.String sExpectedSyntax
protected java.lang.String xxxsFoundedSyntax
private int maxParseResultEntriesOnError
java.util.ArrayList<ZbnfParseResultItem> listParseResultOnError
protected long posRightestError
java.lang.String sCommentStringStart
java.lang.String sCommentStringEnd
boolean bStoreComment
java.lang.String sEndlineCommentStringStart
protected java.lang.String sInputEncodingKeyword
$inputEncodingKeyword="...". this variable is set.
The content are not used inside the parser itself, but may be requested outside.
protected java.lang.String sInputEncoding
$inputEncoding="...". this variable is set.
The content are not used inside the parser itself, but may be requested outside.
boolean bStoreEndlineComment
java.lang.String sWhiteSpaces
boolean bStoreNewline
boolean bStoreOneSpaceOnWhitespaces
boolean bStoreWhiteSpaces
private java.nio.charset.Charset charsetInput
private ZbnfParserStore parserStoreTopLevel
| Constructor Detail |
|---|
public ZbnfParser(Report report)
report - A report output
public ZbnfParser(Report report,
int maxParseResultEntriesOnError)
report - A report outputmaxParseResultEntriesOnError - if 0 than no parse result is stored.
If >0, than the last founded parse result is stored to support better analysis of syntax errors,
but the parser is slower.| Method Detail |
|---|
public void setSyntax(java.lang.String syntax)
throws java.text.ParseException
syntax - The ZBNF-Syntax.
java.text.ParseException
protected void setSyntax(java.io.InputStream input)
throws java.text.ParseException
java.text.ParseException
public void setSyntax(java.io.File fileSyntax)
throws java.nio.charset.IllegalCharsetNameException,
java.nio.charset.UnsupportedCharsetException,
java.io.FileNotFoundException,
java.io.IOException,
java.text.ParseException
java.nio.charset.IllegalCharsetNameException
java.nio.charset.UnsupportedCharsetException
java.io.FileNotFoundException
java.io.IOException
java.text.ParseException
public void setSyntax(StringPart syntax)
throws java.text.ParseException
SyntaxPrescript.
The class SyntaxPrescript is private inside the Parser, but its matter of principle may be
explained here. SyntaxPrescript contains a list of elements (listSyntaxElements)
or a list of such listSyntaxElements.
The list of listSyntaxElements is used if there are some alternatives. listSyntaxElements contains
objects of type String, SyntaxPrescript,
Component or Repetition. It is the sequence of
syntax elements of one syntax-path in ZBNF. An object of type String represents a
terminal symbol (constant string).
An element of SyntaxPrescript is an option construction [...|..|..]
or also a simple option [...]. The Repetition represents the
{...?...}-construction. A Repetition contains one or two objects
of type SyntaxPrescript for the forward and optional backward syntax. This syntax-prescripts
may be build complexly in the same way.Component in the listSyntaxElements represents a construction
<...?...>. It may contained the semantic information, it may containded a reference
to another SyntaxPrescript if there is required in the wise <syntax....
It is also built if a construction of kind <!regex...,
<$..., <#... or such else is given.SyntaxPrescript is passed by syntax test, the right way is searched,
see method parse()
syntax - The syntax in ZBNF-Format.
java.text.ParseException - If any wrong syntax is containing in the ZBNF-string. A string-wise information
of the error location is given.
private void setSyntax(StringPart syntax,
java.lang.String sDirParent)
throws java.text.ParseException,
java.nio.charset.IllegalCharsetNameException,
java.nio.charset.UnsupportedCharsetException,
java.io.FileNotFoundException,
java.io.IOException
java.text.ParseException
java.nio.charset.IllegalCharsetNameException
java.nio.charset.UnsupportedCharsetException
java.io.FileNotFoundException
java.io.IOException
private void importScript(java.lang.String sFile,
java.lang.String sDirParent)
throws java.nio.charset.IllegalCharsetNameException,
java.nio.charset.UnsupportedCharsetException,
java.io.FileNotFoundException,
java.io.IOException,
java.text.ParseException
java.nio.charset.IllegalCharsetNameException
java.nio.charset.UnsupportedCharsetException
java.io.FileNotFoundException
java.io.IOException
java.text.ParseException
public void setSkippingComment(java.lang.String sCommentStringStart,
java.lang.String sCommentStringEnd,
boolean bStoreComment)
sCommentStringStart - The start chars of comment string, at example '/ *'sCommentStringEnd - The end chars of comment string, at example '* /'bStoreComment - If it is true, the comment string will be stored in the ParserStrore
and can be evaluated from the user.
public void setSkippingEndlineComment(java.lang.String sCommentStringStart,
boolean bStoreComment)
sCommentStringStart - The start chars of comment string to end of line, at example '/ /'bStoreComment - If it is true, the comment string will be stored in the ParserStrore
and can be evaluated from the user.public void setWhiteSpaces(java.lang.String sWhiteSpaces)
sWhiteSpaces - Chars there are recognize as white space.setSyntax(String).public void setLinemode(boolean bTrue)
setSyntax(String).
public void setReportIdents(int identError,
int identInfo,
int identComponent,
int identFine)
identError - ident for error and warning outputs.identInfo - ident for progress information output.identComponent - ident for output if a component is parsingidentFine - ident for fine parsing outputs.public boolean parse(java.lang.String input)
input -
public boolean parse(StringPart input)
SyntaxPrescript are tested. If a matching path
is found, the method returns true, otherwise false. The result of parsing is stored inside the parser
(private internal class ParserStore).
To evaluate the parse result see getFirstParseResult().
input - The source to be parsed.
public boolean parse(StringPart input,
java.util.List<java.lang.String> additionalInfo)
parse(StringPart), but write additional semantic informations
into the first parse result (into the top level component).
input - The text to parseadditionalInfo - Pairs of semantic idents and approriate information content.
The elements [0], [2] etc. contains the semantic identifier
whereas the elements [1], [3] etc. contains the information content.
private ZbnfSyntaxPrescript searchSyntaxPrescript(java.lang.String sSyntax)
public void reportSyntax(Report report,
int reportLevel)
public void reportStore(Report report,
int reportLevel,
java.lang.String sTitle)
parseResult: <?semanticIdent> Component parseResult: <?semanticIdent> ident="foundedString" parseResult: <?semanticIdent> number=foundedNumber parseResult: </?semanticIdent> ComponentEvery line is exactly one entry in the parsers store.
report - The report output instancereportLevel - level of report. This level is shown in output.
If the current valid reportLevel of report is less than this parameter, no action is done.
public void reportStore(Report report,
int reportLevel)
public void reportStore(Report report)
report - The report output instance.reportStore(Report report, int reportLevel)}.
private int reportStoreComponent(ZbnfParseResultItem parseResultItem,
Report report,
int level,
ZbnfParseResultItem parent,
int reportLevel)
parseResultItem - The first item to report, it is the next item behind componentes first (head-) item, if it is a component.report - The report system.level - Level of nested componentesparent - If not null, the inner items of parent component are reported.
public java.lang.String getInputEncodingKeyword()
$inputEncodingKeyword="...". in the syntax prescript or null it no such entry is given.
public java.nio.charset.Charset getInputEncoding()
$inputEncoding="...". in the syntax prescript or null it no such entry is given.
public java.lang.String getExpectedSyntaxOnError()
public java.lang.String getLastFoundedResultOnError()
public java.lang.String getFoundedInputOnError()
public long getInputPositionOnError()
protected void throwSyntaxErrorException(java.lang.String text)
throws java.text.ParseException
text - leading text
java.text.ParseException - immediate.public java.lang.String getSyntaxErrorReport()
public ZbnfParseResultItem getFirstParseResult()
public java.util.TreeMap<java.lang.String,java.lang.String> getXmlnsFromSyntaxPrescript()
public boolean setStoringConstantSyntax(boolean bStore)
bStore - true if they should strored, false if not.
private void stop()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||