public class ZbnfParser
extends java.lang.Object
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 StringPartScan
.
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.Modifier and Type | Class and Description |
---|---|
static class |
ZbnfParser.Args |
(package private) static class |
ZbnfParser.ForkPoint
Position where an option should be parsed.
|
(package private) static class |
ZbnfParser.LogParsing
This class contains some information to create a log output which logs the parsing process.
|
(package private) class |
ZbnfParser.LogZbnfParser
Class for logging.
|
(package private) static class |
ZbnfParser.ParseResultlet
Element of a Parse result for a part of the syntax.
|
(package private) class |
ZbnfParser.PrescriptParser
Class to organize parsing of a component with a own prescript.
|
Modifier and Type | Field and Description |
---|---|
(package private) java.util.TreeMap<java.lang.String,ZbnfParser.ParseResultlet> |
alreadyParsedCmpn
Already parsed components with the same input text which should be requested in another context.
|
ZbnfParser.Args |
args |
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 ZbnfParserStore.BuilderTreeNodeXml |
builderTreeNodeXml |
private java.nio.charset.Charset |
charsetInput |
protected int |
columnError
The lineError and columnError will be set if the input supports it, see
StringPart.getLineAndColumn(int[]) . |
(package private) int |
dbgLineSyntax |
(package private) int |
dbgPosFrom |
(package private) int |
dbgPosTo |
protected int |
idReportBranchParsing |
protected int |
idReportComponentParsing |
protected int |
idReportError |
protected int |
idReportInfo |
protected int |
idReportParsing
The ident to report the progress of parsing.
|
protected java.util.Map<java.lang.String,java.lang.String> |
idxMissingPrescripts |
protected int |
lineError
The lineError and columnError will be set if the input supports it, see
StringPart.getLineAndColumn(int[]) . |
(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 all syntax definitons (syntax components).
|
private ZbnfParser.LogParsing |
log |
(package private) ZbnfParser.LogZbnfParser |
log1 |
private ZbnfSyntaxPrescript |
mainScript
The main syntax prescript set from
setSyntax(StringPart) . |
private int |
maxParseResultEntriesOnError
Maximum number of shown parsing results on error.
|
(package private) static int |
mXmlSrcline_xmlWrmode |
(package private) static int |
mXmlSrctext_xmlWrmode |
protected int |
nLevelReportBranchParsing |
protected int |
nLevelReportComponentParsing |
protected int |
nLevelReportError |
protected int |
nLevelReportInfo |
protected int |
nLevelReportParsing |
protected int |
nReportLevel
The current report level.
|
private ZbnfParserStore |
parserStoreTopLevel
The actual parse result buffer.
|
protected long |
posRightestError
The position of the most right parse fault.
|
protected ZbnfParser.PrescriptParser |
prescriptParserTopLevel |
protected LogMessage |
report
To LogMessage 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 |
sFileError
The file or name of the
StringPart.getInputfile() which was parsed on the rightest error position. |
protected java.lang.String |
sInputEncoding
If the syntax prescript contains
$inputEncoding="...". |
protected java.lang.String |
sInputEncodingKeyword
If the syntax prescript contains
$inputEncodingKeyword="...". |
(package private) java.lang.String |
sInputMostRightError |
protected java.lang.CharSequence |
sRightestError
The string and position found on the rightest position of an parse fault.
|
static java.lang.String |
sVersion
Version, history and license.
|
(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 and Description |
---|
ZbnfParser(LogMessage report)
Creates a empty parser instance.
|
ZbnfParser(LogMessage report,
int maxParseResultEntriesOnError)
Creates a empty parser instance.
|
ZbnfParser(LogMessage report,
ZbnfParser.Args args)
Creates a empty parser instance.
|
Modifier and Type | Method and Description |
---|---|
java.lang.StringBuilder |
buildFoundedInputOnError()
Returns about 50 chars of the input string founded at the parsing
error position.
|
void |
clean()
Cleans used memory after evaluation of the parse result.
|
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()
Invokes
buildFoundedInputOnError() with String as return value. |
int |
getInputColumnOnError() |
java.nio.charset.Charset |
getInputEncoding()
Returns the setting of
$inputEncoding="...". |
java.lang.String |
getInputEncodingKeyword()
Returns the setting of
$inputEncodingKeyword="...". |
java.lang.String |
getInputFileOnError() |
int |
getInputLineOnError() |
long |
getInputPositionOnError()
Returns the position of error in input string.
|
java.lang.String |
getLastFoundedResultOnError()
Returns the up to now founded result on error position.
|
TreeNode_ifc<XmlNodeSimple<ZbnfParseResultItem>,ZbnfParseResultItem> |
getResultNode() |
XmlNode |
getResultTree()
Returns the XML-like result tree.
|
java.lang.String |
getRightestInputOnError() |
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) |
(package private) static java.lang.CharSequence |
inputCurrent(StringPartScan input) |
ZbnfSyntaxPrescript |
mainScript() |
boolean |
parse(java.lang.String input)
Parses a given Input and produces a parse result.
|
boolean |
parse(StringPartScan input)
parses a given Input and produces a parse result.
|
boolean |
parse(StringPartScan 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). |
boolean |
parseFile(java.io.File fInput)
Parses a given file with standard encoding, produces a parse result.
|
boolean |
parseFile(java.io.File fInput,
int maxBuffer,
java.lang.String sEncodingDetect,
java.nio.charset.Charset charset)
Parses a given file with standard encoding, produces a parse result.
|
boolean |
parseFileFromJar(java.lang.Class<?> clazz,
java.lang.String pathInJarFromClazz,
int maxSize)
Parsed a content which is stored as resource in a jar file.
|
void |
reportStore(LogMessage report)
Reports the whole content of the parse result in the LogMessage.fineInfo-level.
|
void |
reportStore(LogMessage report,
int reportLevel) |
void |
reportStore(LogMessage report,
int reportLevel,
java.lang.String sTitle)
Reports the whole content of the parse result.
|
private int |
reportStoreComponent(ZbnfParseResultItem parseResultItem,
LogMessage report,
int level,
ZbnfParseResultItem parent,
int reportLevel)
Inner method to report the content of the parse result
|
void |
reportSyntax(LogMessage report,
int reportLevel)
Reports the syntax.
|
protected ZbnfSyntaxPrescript |
searchSyntaxPrescript(java.lang.String sSyntax) |
void |
setDebugPosition(int from,
int to,
int lineSyntax)
Sets info for debug break, see using of
dbgPosFrom etc. |
void |
setLinemode(boolean bTrue)
Sets the line mode or not.
|
void |
setLogComponents(java.lang.Appendable out)
Optional setting to log which syntax components are entered on which input position.
|
void |
setMainSyntax(java.lang.String ident)
Sets another syntax rule as the first entry in the given syntax.
|
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.lang.CharSequence syntax)
Sets the syntax from given string.
|
void |
setSyntax(java.io.File fileSyntax) |
void |
setSyntax(StringPartScan syntax)
Sets the syntax from given String.
|
void |
setSyntax(StringPartScan syntax,
java.lang.String sDirImport)
Sets the syntax
|
void |
setSyntaxFile(java.io.File fileSyntax)
Sets the syntax from a file.
|
void |
setSyntaxFromJar(java.lang.Class<?> clazz,
java.lang.String pathInJarFromClazz)
Read syntax from a resource (file inside jar archive).
|
void |
setSyntaxString(java.lang.CharSequence syntax)
Sets the syntax from given string.
|
void |
setWhiteSpaces(java.lang.String sWhiteSpaces)
Sets the chars which are recognized as white spaces.
|
void |
setXmlSrcline(boolean bValue)
Sets the mode of output source line and column in XML.
|
void |
setXmlSrctext(boolean bValue)
Sets the mode of output source line and column in XML.
|
private void |
stop()
It's a debug helper.
|
java.util.TreeMap<java.lang.String,ZbnfSyntaxPrescript> |
subPrescripts()
Returns the index of all sub prescripts for checking.
|
protected void |
throwSyntaxErrorException(java.lang.String text)
throws a ParseException with the infos of syntax error from last parsing.
|
void |
writeResultAsTextList(java.lang.Appendable out) |
void |
writeSyntaxStruct(java.lang.Appendable out)
Writes the syntax (
setSyntax(StringPartScan, String)
in a simple text file using ZbnfSyntaxPrescript.toString()
via ZbnfSyntaxPrescript.writeSyntaxStruct(Appendable, int)
in a recursively iteration. |
public static final java.lang.String sVersion
clean()
to remove temporary stuff.
ZbnfSyntaxPrescript.EType.kStoreSrc
:
This seems a good opportunity to store the source to an element or component.
ZbnfParser.PrescriptParser.SubParser.posStoreSrc
and ZbnfParser.PrescriptParser.SubParser.semanticStoreSrc
to save the start position for the source
SubParser#parsePrescript(ZbnfSyntaxPrescript, ParseResultItemImplement, boolean, boolean, int)
:
create the element with the given semantic if bOk.
<component?""semantic
( ZbnfSyntaxPrescript.bStoreAsString
is set), then writes the parse result of this component
in ZbnfParserStore.ParseResultItemImplement.sInput
, now also inside the component.
There it is useful to evaluate, used for parsed hex numbers, " 0x000e" is the source, and 14 is the parse result,
to less information without knowledge of the source. Used first time for Java2Vhdl translator.
StringPartScan.scanDigits(int, int, String, String[])
with destination of source and stores this source string in ZbnfParserStore.ParseResultItemImplement.sInput
.
It is offered, what to do with it depends on evaluation of the parse result. See change in ZbnfJavaOutput
.
writeSyntaxStruct(Appendable)
as a new feature, should be existing from beginning...
parse(StringPartScan, List)
: set nReportLevel
to 0, switch off the old report strategy,
because the new one with setLogComponents(Appendable)
seems to be better. In comparison to =3 30% lesser calculation time.
ZbnfParser(LogMessage, Args)
has not reported "found before:".
The size of the ZbnfParser.Args.maxParseResultEntriesOnError
was not gotten correctly.
setLogComponents(Appendable)
new feature tested
sInputMostRightError
set on any parser non matching as most right. The problem was, the input file was not seen,
only its line and column, but this is not reliable at all times.
SubParser#parseRepetition(ZbnfSyntaxPrescript, ZbnfSyntaxPrescript, ParseResultItemImplement, boolean, int)
:
regard {
as type designation of the created parse result, stores in ZbnfSyntaxPrescript.componentSyntax
for usage in ZbnfJavaOutput
. Not for the parsing process itself used.
SubParser#parseItem(ZbnfSyntaxPrescript, ParseResultItemImplement, boolean, int)
If a component is necessary, search only one time and store in ZbnfSyntaxPrescript.componentSyntax
for further usage.
It saves time. The reference is also used for ZbnfJavaOutput
.
SubParser#addResultOrSubsyntax(CharSequence, long, int, int, String, String, ParseResultItemImplement, ZbnfSyntaxPrescript)
ZbnfParserStore.ParseResultItemImplement
.
SubParser#parseRepetition(ZbnfSyntaxPrescript, ZbnfSyntaxPrescript, ParseResultItemImplement, boolean, int)
now can create a result item for the whole repetition inclusively backward items.
This is necessary for a proper evaluation. It is written in form {*semantic:superType>
(supertype is optional). The * means, 'more' or 'all' in the curly braces, also the backward path.
writeResultAsTextList(Appendable)
calls feature in ZbnfParserStore.writeContentAsList(Appendable)
ZbnfSyntaxPrescript.EType.kOnlyMarker
, ignore this item.
setSyntaxFromJar(Class, String)
and parseFileFromJar(Class, String, int)
ZbnfSyntaxPrescript.bDebugParsing
,
hence it is more simple to test a Zbnf script.
ZbnfParser.Args.bUseResultlet
is set, default is false.
ZbnfParser(LogMessage, Args)
new ZbnfParser.Args.bUseResultlet
getResultNode()
is called, not unnecessarily in any case.
The algorithm for reusing already parsed results does not use the XML tree.
setSyntax(CharSequence)
formally with CharSequence instead String, more common useable, especially from new FileFunctions.readInJar(Class, String, String)
setMainSyntax(String)
not only the first entry can be the the main rule. Used to parse inner Syntax in XML (for IEC 61499).
.indent=-3
-attribute.
The primary error was using the faulty ZbnfSyntaxPrescript
instance to store the parse result in parseSub(...)
.
Because of change in 2019-05 accidentally or experimentally the current used prescript are used for the parse result instead the calling prescript item.
It is now tested with GenZbnfJavaData
and FBCL, that it is not comprehensible for necessity.
The parentSyntaxItem
(calling level) contains the correct semantic (often same as current syntax prescript but not in any case)
and especially for JZtxtcmd indent on texts the attribute for indent, or some more information.
ZbnfSyntaxPrescript.EType.kTerminalSymbolInComment
is moved before checking
some nested syntax to set the posInput (local variable).
PrescriptParser.SubParser#parseSub(ZbnfSyntaxPrescript, String, int, ZbnfSyntaxPrescript, String, ParseResultItemImplement, boolean, ZbnfParserStore)
Some gardening in arguments.
[<?result>...
It was faulty in some cases in comparison with ZBNF/testAllConcepts - test.
The result should be stored as ZbnfParserStore.ParseResultItemImplement.parsedString
and not as sInput.
See changes in ZbnfParserStore.ParseResultItemImplement.getText()
. It should not be stored if a result string is set already
with {<?semantic=> ...
for an empty element per repetition, for example.args
ZbnfParser.Args.bStoreInputForComponent
is true. it can be set either immediately in a JZtxtcmd Script or with public access,
or ZbnfParser.Args
should be used in a command line environment and set via ctor (TODO)
setDebugPosition(int, int, int)
for low level source debugging (Eclipse) on special problems.
ZbnfSyntaxPrescript.EType
as enum. It is not a functional change
getResultNode()
. To evaluate the result with JZtxtcmd immediately without interim store.
ZbnfSyntaxPrescript
syntax item is stored in the ZbnfParserStore.ParseResultItemImplement
.
ZbnfParser.PrescriptParser.srcLineOption
is never used now. The idea in 2015 was: supply position for text indentation
though the < subtext> item was written after a <:> (JZcmd, jzTc), the position of the options may be usefully. But that is a non-simple coherence.
Now the position of a syntax item in the parsed text is written immediately in the parse result. The correction of the indentation is defined by an attribute
in the syntax. See JZtxtcmdSyntax
element \\<:\\>< textExpr?.indent=-3>
StringPartScan.scanStart()
in ZbnfParser.PrescriptParser.SubParser.parseTerminalSymbol(ZbnfSyntaxPrescript, org.vishia.zbnf.ZbnfParserStore.ParseResultItemImplement)
.
In follow of that an error in the terminal text shows an faulty position (the position from any scanStart() before).
An syntax error in the parsed text was not shown with the exact position.
<...?%...> in Syntax, then ZbnfSyntaxPrescript.bDebugParsing
is set for this item.
It can be tested here to set a specific debug breakpoint for parsing this element. Only for special debugging problems.
- 2015-07-04 Hartmut bugfix of change on 2015-06-14: It should check kTerminalSymbolInComment if such an symbol is parsed
inside a part with
<$NoWhiteSpaces>
- 2015-06-14 Hartmut chg: Writes the start of option parsing in log, "Opti" on level 5. Writes the recursion depths in log.
Note: The level
nLevelReportParsing
respectively all source parts "report.report..." outside of ZbnfParser.LogParsing
should be removed. They are not reviewed, the usage of ZbnfParser.LogParsing
is better.
- 2015-06-14 Hartmut new: distinguishs between
ZbnfSyntaxPrescript#kTerminalSymbolInComment
and ZbnfSyntaxPrescript#kTerminalSymbol
.
- 2015-06-07 Hartmut chg:
ZbnfParser.PrescriptParser.srcLineOption
etc. created and filled. If given it is the src position for a component.
- 2015-06-07 Hartmut chg: Improved setting line, column and position in parse result items.
- 2015-06-06 Hartmut chg: Showing components in logfile now from left to right root to special, may be better to read.
- 2015-06-06 Hartmut chg: showing position in String on error additional to line and column, need for error analyzing.
- 2015-06-06 Hartmut chg:
SubParser#parseSub(ZbnfSyntaxPrescript, String, int, String, boolean, ZbnfParserStore)
gets the syntaxPrescript as argument, not a instance variable. Therewith it is not necessary to have an own instance for parsing
some alternative options. Because that is the most frequently parse task, it should save calculation time to do so.
Furthermore ZbnfSyntaxPrescript#kAlternativeOptionCheckEmptyFirst
is handled in the core parser routine
in SubParser#parseSub(ZbnfSyntaxPrescript, String, int, String, boolean, ZbnfParserStore)
respectively in the new sub routine in parseSub(...) SubParser#parsePrescript(List, boolean)
.
SubParser#idxPrescript
not as instance variable but as stack local variable.
- 2015-06-04 Hartmut chg: Sets line and column of a component from the first read item insert the component, after whiltespaces.
That is important for JZcmd to detect the indent position of texts.
- 2014-12-14 Hartmut chg: Now returns the line and column and the name of the input file on error if that information are available.
There are available for a
StringPartFromFileLines
which is used usual as input for the parser.
Changed routines: getSyntaxErrorReport()
, getFoundedInputOnError()
, new: buildFoundedInputOnError()
.
- 2014-06-17 Hartmut new:
setXmlSrcline(boolean)
and setXmlSrctext(boolean)
to control
whether srcline="xx" and srctext="text" will be written to a XML output
- 2014-05-23 Hartmut chg: use
StringPart.getLineAndColumn(int[])
instead getLineCt() and StringPart.getCurrentColumn()
because it is faster.
- 2014-05-22 Hartmut new: Save srcFile in
ZbnfParserStore.ParseResultItemImplement.sFile
,
for information in written results, especially with ZbnfJavaOutput
.
- 2014-03-21 Hartmut new:
setSyntaxFile(File)
and #setSyntaxString(String)
for ambiguous names called from a JZcmd script.
- 2014-03-21 Hartmut bugfix: Parsing kStringUntilEndStringWithIndent and regular expression: There was a check
'if(sSemanticForStoring != null)' before calling addResultOrSubsyntax(...), therefore
<*{ * }|* /?!test_description>
has not write the result of the sub syntax.
It is not correct. Originally there was set sSrc and addResultOrSubsyntax(...) was invoked if(sSrc !=null).
That code is reconstructed again.
- 2014-01-23 Hartmut chg:
SubParser#parseSub(StringPartScan, String, int, String, boolean, ZbnfParserStore)
:
Whitespaces skipped before parserStoreInPrescript.addAlternative(...)
is called because the position in input
should be stored in the alternative ParseResultItem after the whitespaces. Especially the correct line
should be noted.
- 2014-01-23 Hartmut chg:
SubParser#parseWhiteSpaceAndCommentOrTerminalSymbol(String, ZbnfParserStore)
:
the parseResult
argument is used only if sConstantSyntax
is not null. Changed consequently.
Now it is possible to invoke this routine with parseWhiteSpaceAndCommentOrTerminalSymbol(null, null)
to only skip white spaces and comments. Therefore ZbnfParser.PrescriptParser.SubParser.parseWhiteSpaceAndComment()
is possible
without the 'parseResult' argument.
- 2014-01-23 Hartmut bugfix: The
<*{ }>
for indented lines does not work. Testing and fixing.
- 2014-01-01 Hartmut new: Line number transfer to parse result items. Idea TODO: transfer the line numbers only
on finish of parsing, store position in input file while parsing: There are some more items stored in the parse process
than remain on finish. Getting line numbers form
org.vishia.util.StringPartFromFileLines#getLineAndColumn(column)
is a binary search process of association position to line numbers. It should only be done on end only for the
remaining parse result items. Time measurement: Parsing of about 30 Headerfiles with line numbers: 15 seconds,
without line numbers: 14 second.
- 2013-12-06 Hartmut nice fix: trim spaces in $comment and $endlineComment. A user may write white spaces, it didn't recognize comments.
Now white spaces are admissable.
- 2013-09-02 Hartmut TODO forex "[{
= }] cmd " saves the ZbnfParser.PrescriptParser.parseResultToOtherComponent
of "assign"
because that SubParser#parse_Component(StringPartScan, int, String, String, boolean, boolean, boolean)
is ok. But the outer level "{ ... = }"
fails because the "=" is not present. In this case the ZbnfParser.PrescriptParser.parseResultToOtherComponent
should be removed if it comes
from an inner SubParser which is not used. The solution should be: The parseResultToOtherComponent should be an attribute of ZbnfParser.PrescriptParser.SubParser
instead the ZbnfParser.PrescriptParser
, the ZbnfParser.PrescriptParser
should know it via a List and all levels of SubParser should
have a List for its own or inner Result items for other component. If a SubParser's syntax does not match, all ParserStores,
inclusive the inner ones, can and should be removed.
- 2013-02-26 Hartmut bugfix:
PrescriptParser#parsePrescript1(String, ZbnfParseResultItem, ZbnfParserStore, ZbnfParserStore, boolean, int)
while storing ParseResultlet#xmlResult
in alreadyParsedCmpn
: If the result is empty, the resultlet
should be stored with an xmlResult=null (nothing was created), but the syntax is ok. There are some syntax checks
without result possible.
- 2013-02-12 Hartmut chg:
getResultTree()
returns now the interface reference XmlNode
instead the
implementation instance reference XmlNodeSimple
. The implementation is the same.
All references are adapted, especially ParseResultlet#xmlResult
- 2013-01-18 Hartmut chg, new: Log-output improved. New inner class
ZbnfParser.LogParsing
.
- 2013-01-04 Hartmut new
alreadyParsedCmpn
. It may be speed up the parsing process but only if the same component
is requested at the same position inside another component. It is not used yet.
Todo: position of text for 2012-11-02 Hartmut new local class ZbnfParser.ParseResultlet
, the ZbnfParser.PrescriptParser
contains a reference to it.
The resultlet is the first action to save gotten parse results though the result is not convenient in the current context.
This result may be re-used later in another context (not programmed yet, only prepared).
In that context any component's result is converted to an XML tree presentation. This may be the new strategy for parse result storing.
- 2012-10-23 Hartmut Supports <* |endstring: The parse result is trimmed without leading and trailing white spaces.
- 2011-10-10 Hartmut bugfix: scanFloatNumber(true). The parser had an exception because more as 5 floats are parsed and not gotten calling
StringPartScan.getLastScannedFloatNumber()
.
- 2011-01-09 Hartmut corr: Improvement of report of parsing: Not the report level
nLevelReportBranchParsing
(set with LogMessage.debug usualy) writes any branch of parsing with ok or error. In that way the working of the parser
in respect to the syntax prescript is able to view. It is if some uncertainty about the correctness of the given syntax is in question.
- 2011-01-09 Creation of this variable to show the changes in the javadoc.
- 2010-05-04 Hartmut: corr: sEndlineCommentStringStart: The \n is not included, it will be skipped either as whitespace or it is necessary for the linemode.
- 2009-12-30 Hartmut: corr: Output info: subParserTopLevel == null, no syntax is now removed.
- 2009-08-02 Hartmut: new: parsing with subSyntax now also available in options writing [!subSyntax> ...].
- 2009-08-02 Hartmut: new: parseExpectedVariant writing [!...] now available. It tests but doesn't processed the content.
- 2009-08-02 Hartmut: new: $Whitespaces= now accepted (it was declared in documentation but not implement).
- 2009-05-31 Hartmut: corr: some changes of report and error output: In both cases the syntax path is written from inner to root,
separated with a +.
In reportlevel 5 (nLevelReportComponentParsing) also the success of parsing terminal symbols are reported,
in the same line after 'ok/error Component'. The reporting of parsing process should be improved furthermore.
- 2009-01-16 Hartmut: new: ZbnfSyntaxPrescript.kFloatWithFactor: Writing <#f*Factor?...> is working now..
corr: Some non-active code parts deleted.
corr: Processing of parse(... additionalInfo) corrected. It was the only one position, where setparseResultsFromOuterLevels() was used. But more simple is:
chg: pass of ParseResult to components is simplified, in the kind like programmed in 2007. The pass of parse-Results through some components in deeper levels ins't able now,
but that feature causes falsity by using.
- 2008-03-28 JcHartmut: The ParserStore is not cleared, only the reference is assigned new.
So outside the ParserStore can be used from an older parsing.
- 2006-12-15 JcHartmut: regular expressions should be handled after white spaces trimming, error correction.
- 2006-06-00 JcHartmut: a lot of simple problems in developemnt.
- 2006-05-00 JcHartmut: creation
private static final java.lang.String sEmpty
public final ZbnfParser.Args args
static final int mXmlSrcline_xmlWrmode
static final int mXmlSrctext_xmlWrmode
int dbgPosFrom
int dbgPosTo
int dbgLineSyntax
protected final LogMessage report
protected int nReportLevel
protected int nLevelReportParsing
protected int nLevelReportComponentParsing
protected int nLevelReportInfo
protected int nLevelReportError
protected int nLevelReportBranchParsing
protected int idReportParsing
protected int idReportComponentParsing
protected int idReportBranchParsing
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 java.lang.CharSequence sRightestError
protected java.lang.String sExpectedSyntax
protected java.lang.String xxxsFoundedSyntax
private int maxParseResultEntriesOnError
private final ZbnfParser.LogParsing log
java.util.ArrayList<ZbnfParseResultItem> listParseResultOnError
protected long posRightestError
protected int lineError
StringPart.getLineAndColumn(int[])
.
It is necessary to report a parsing error.protected int columnError
StringPart.getLineAndColumn(int[])
.
It is necessary to report a parsing error.java.lang.String sInputMostRightError
protected java.lang.String sFileError
StringPart.getInputfile()
which was parsed on the rightest error position.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
protected java.util.Map<java.lang.String,java.lang.String> idxMissingPrescripts
private ZbnfParserStore parserStoreTopLevel
private final ZbnfParserStore.BuilderTreeNodeXml builderTreeNodeXml
final java.util.TreeMap<java.lang.String,ZbnfParser.ParseResultlet> alreadyParsedCmpn
final ZbnfParser.LogZbnfParser log1
public ZbnfParser(LogMessage report)
report
- A report outputpublic ZbnfParser(LogMessage 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.public ZbnfParser(LogMessage report, ZbnfParser.Args args)
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.public void setLogComponents(java.lang.Appendable out)
out
- public void setSyntax(java.lang.CharSequence syntax) throws java.text.ParseException
syntax
- The ZBNF-Syntax.java.text.ParseException
public void setSyntaxString(java.lang.CharSequence syntax) throws java.text.ParseException
setSyntax(File)
and #setSyntax(String)
.syntax
- The ZBNF-Syntax.java.text.ParseException
public void setSyntaxFile(java.io.File fileSyntax) throws java.nio.charset.IllegalCharsetNameException, java.nio.charset.UnsupportedCharsetException, java.io.FileNotFoundException, java.io.IOException, java.text.ParseException
setSyntax(File)
and #setSyntax(String)
.fileSyntax
- The file which contains the syntax prescription.java.nio.charset.IllegalCharsetNameException
java.nio.charset.UnsupportedCharsetException
java.io.FileNotFoundException
java.io.IOException
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 setSyntaxFromJar(java.lang.Class<?> clazz, java.lang.String pathInJarFromClazz) throws java.io.IOException, java.nio.charset.IllegalCharsetNameException, java.nio.charset.UnsupportedCharsetException, java.text.ParseException
clazz
- A class in any jar, from there the relative path to the pathInJar is built.
Usually the clazz should be the output data clazz. But it is a user decision.pathInJar
- relative Path from clazz.
Usually the syntax should be in the same directory as the output data class. Then this is only the file name.
If the file is stored in a pjava.io.IOException
java.text.ParseException
java.nio.charset.UnsupportedCharsetException
java.nio.charset.IllegalCharsetNameException
public void setSyntax(StringPartScan 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.public void setSyntax(StringPartScan syntax, java.lang.String sDirImport) throws java.text.ParseException, java.nio.charset.IllegalCharsetNameException, java.nio.charset.UnsupportedCharsetException, java.io.FileNotFoundException, java.io.IOException
syntax
- The syntax, may be read from any file or from a String, use new StringPart(...)sDirImport
- If the syntax contains a $import statement, use this directory as current dir to search the file.java.text.ParseException
java.nio.charset.IllegalCharsetNameException
java.nio.charset.UnsupportedCharsetException
java.io.FileNotFoundException
java.io.IOException
public void setMainSyntax(java.lang.String ident) throws java.text.ParseException
setSyntax(File)
routines should be called before.ident
- syntax rule, java.text.ParseException
public void writeSyntaxStruct(java.lang.Appendable out) throws java.io.IOException
setSyntax(StringPartScan, String)
in a simple text file using ZbnfSyntaxPrescript.toString()
via ZbnfSyntaxPrescript.writeSyntaxStruct(Appendable, int)
in a recursively iteration. It is interesting to see. Can be improved for details.
This routine organizes the main and all sub syntax components known on parser level.
This feature may be proper from beginning.out
- to a Writer, StringBuilder or what ever.java.io.IOException
- from Appendable.append(char)
public ZbnfSyntaxPrescript mainScript()
public java.util.TreeMap<java.lang.String,ZbnfSyntaxPrescript> subPrescripts()
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 setDebugPosition(int from, int to, int lineSyntax)
dbgPosFrom
etc.from
- The absolute char positon, not the line, it is outputted on error reportsto
- if the current position is between from and to, the break condition met.lineSyntax
- Additional condition: Only if the semantic item on this line is used. It is the definition::= line in the zbnf script.
Use 0 if it should be inactive.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 setXmlSrcline(boolean bValue)
setSyntax(String).
public void setXmlSrctext(boolean bValue)
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 parseFile(java.io.File fInput, int maxBuffer, java.lang.String sEncodingDetect, java.nio.charset.Charset charset) throws java.nio.charset.IllegalCharsetNameException, java.nio.charset.UnsupportedCharsetException, java.io.FileNotFoundException, java.io.IOException
fInput
- The file to readmaxBuffer
- The maximum of length of the associated StringBuffer.sEncodingDetect
- If not null, this string is searched in the first line,
read in US-ASCII or UTF-16-Format. If this string is found, the followed
string in quotation marks or as identifier with addition '-' char is read
and used as charset name. If the charset name is failed, a CharsetException is thrown.
It means, a failed content of file may cause a charset exception.charset
- If not null, this charset is used as default, if no other charset is found in the files first line,
see param sEncodingDetect. If null and not charset is found in file, the systems default charset is used.getSyntaxErrorReport()
java.io.FileNotFoundException
- If the file is not foundjava.io.IOException
- If any other exception is thrownjava.io.IOException
java.io.FileNotFoundException
java.nio.charset.UnsupportedCharsetException
java.nio.charset.IllegalCharsetNameException
public boolean parseFile(java.io.File fInput) throws java.nio.charset.IllegalCharsetNameException, java.nio.charset.UnsupportedCharsetException, java.io.FileNotFoundException, java.io.IOException
fInput
- getSyntaxErrorReport()
java.io.IOException
java.io.FileNotFoundException
java.nio.charset.UnsupportedCharsetException
java.nio.charset.IllegalCharsetNameException
public boolean parseFileFromJar(java.lang.Class<?> clazz, java.lang.String pathInJarFromClazz, int maxSize) throws java.io.IOException
clazz
- A class in any jar, from there the relative path to the pathInJar is built.
Usually the clazz should be the output data clazz. But it is a user decision.pathInJar
- relative Path from clazz.
Usually the syntax should be in the same directory as the output data class. Then this is only the file name.
If the file is stored in a pgetExpectedSyntaxOnError()
etc.java.io.IOException
public boolean parse(java.lang.String input)
parse(StringPartScan)
.input
- public boolean parse(StringPartScan 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(StringPartScan 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.java.io.IOException
protected ZbnfSyntaxPrescript searchSyntaxPrescript(java.lang.String sSyntax)
public void reportSyntax(LogMessage report, int reportLevel)
public void reportStore(LogMessage 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(LogMessage report, int reportLevel)
public void reportStore(LogMessage report)
report
- The report output instance.reportStore(LogMessage report, int reportLevel)}.
private int reportStoreComponent(ZbnfParseResultItem parseResultItem, LogMessage 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.StringBuilder buildFoundedInputOnError()
public java.lang.String getFoundedInputOnError()
buildFoundedInputOnError()
with String as return value.
If possible use only buildFoundedInputOnError()
if a CharSequence is sufficient, which are processed in this time.public long getInputPositionOnError()
public java.lang.String getRightestInputOnError()
public int getInputLineOnError()
public int getInputColumnOnError()
public java.lang.String getInputFileOnError()
protected void throwSyntaxErrorException(java.lang.String text) throws java.text.ParseException
text
- leading textjava.text.ParseException
- immediate.public java.lang.String getSyntaxErrorReport()
public ZbnfParseResultItem getFirstParseResult()
public void writeResultAsTextList(java.lang.Appendable out) throws java.io.IOException
java.io.IOException
public XmlNode getResultTree()
XmlNodeSimple
can be written as XML textfile or converted to a Java-XML-format (TODO)
using @org.vishia.xmlSimple.SimpleXmlOutputter
public TreeNode_ifc<XmlNodeSimple<ZbnfParseResultItem>,ZbnfParseResultItem> getResultNode()
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.public void clean()
private void stop()
static java.lang.CharSequence inputCurrent(StringPartScan input)