Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
65dada02
Commit
65dada02
authored
Jul 12, 2021
by
di68kap
Browse files
some refactoring
parent
51de138c
Changes
6
Hide whitespace changes
Inline
Side-by-side
CHANGES.txt
View file @
65dada02
...
...
@@ -39,7 +39,7 @@ DHParser Version 0.9.4 (6.10.2020)
- language servers can now also be connected via streams, not only tcp
- Rudimentary Language Server example (for Visual Studio Code etc.) in
examples/EBNF added
- faster tree seriali
a
zation with Node.as_sxpr or Node.as_xml
- faster tree serialization with Node.as_sxpr or Node.as_xml
DHParser Version 0.9.3 (23.6.2020)
...
...
examples/FixedEBNF/tst_FixedEBNF_grammar.py
View file @
65dada02
...
...
@@ -52,8 +52,31 @@ def run_grammar_tests(glob_pattern, get_grammar, get_transformer):
return
error_report
def
cpu_profile
(
func
):
import
cProfile
as
profile
import
pstats
pr
=
profile
.
Profile
()
pr
.
enable
()
result
=
func
()
pr
.
disable
()
st
=
pstats
.
Stats
(
pr
)
st
.
strip_dirs
()
st
.
sort_stats
(
'time'
).
print_stats
(
40
)
return
result
if
__name__
==
'__main__'
:
argv
=
sys
.
argv
[:]
try
:
i
=
argv
.
index
(
'--profile'
)
del
argv
[
i
]
access_presets
()
set_preset_value
(
'test_parallelization'
,
False
)
finalize_presets
()
print
(
"Profiling test run..."
)
profile
=
True
except
ValueError
:
profile
=
False
if
len
(
argv
)
>
1
and
sys
.
argv
[
1
]
==
"--debug"
:
DEBUG
=
True
del
argv
[
1
]
...
...
@@ -78,7 +101,11 @@ if __name__ == '__main__':
force
=
False
)
sys
.
path
.
append
(
'.'
)
from
FixedEBNFParser
import
get_grammar
,
get_transformer
error_report
=
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
)
if
profile
:
error_report
=
cpu_profile
(
lambda
:
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
))
else
:
error_report
=
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
)
if
error_report
:
print
(
'
\n
'
)
print
(
error_report
)
...
...
examples/FlexibleEBNF/FlexibleEBNFParser.py
View file @
65dada02
...
...
@@ -88,13 +88,13 @@ def preprocess_new(source):
#
#######################################################################
class
EBNFGrammar
(
Grammar
):
r
"""Parser for a
n
EBNF source file.
class
Flexible
EBNFGrammar
(
Grammar
):
r
"""Parser for a
Flexible
EBNF source file.
"""
countable
=
Forward
()
element
=
Forward
()
expression
=
Forward
()
source_hash__
=
"
039bffeb637f4cf2eca83dd83477b83
a"
source_hash__
=
"
431992357f565327257002ab0af2018
a"
disposable__
=
re
.
compile
(
'component$|pure_elem$|countable$|FOLLOW_UP$|SYM_REGEX$|ANY_SUFFIX$|EOF$'
)
static_analysis_pending__
=
[]
# type: List[bool]
parser_initialization__
=
[
"upon instantiation"
]
...
...
@@ -153,7 +153,7 @@ class EBNFGrammar(Grammar):
sequence
=
Series
(
Option
(
Series
(
Text
(
"§"
),
dwsp__
)),
Alternative
(
interleave
,
lookaround
),
ZeroOrMore
(
Series
(
Retrieve
(
AND
),
dwsp__
,
Option
(
Series
(
Text
(
"§"
),
dwsp__
)),
Alternative
(
interleave
,
lookaround
))))
FOLLOW_UP
=
Alternative
(
Text
(
"@"
),
symbol
,
EOF
)
definition
=
Series
(
symbol
,
Retrieve
(
DEF
),
dwsp__
,
Option
(
Series
(
Retrieve
(
OR
),
dwsp__
)),
expression
,
Retrieve
(
ENDL
),
dwsp__
,
Lookahead
(
FOLLOW_UP
),
mandatory
=
1
)
component
=
Alternative
(
regexp
,
literals
,
procedure
,
Series
(
symbol
,
NegativeLookahead
(
DEF
))
)
component
=
Alternative
(
literals
,
procedure
,
expression
)
directive
=
Series
(
Series
(
Text
(
"@"
),
dwsp__
),
symbol
,
Series
(
Text
(
"="
),
dwsp__
),
component
,
ZeroOrMore
(
Series
(
Series
(
Text
(
","
),
dwsp__
),
component
)),
Lookahead
(
FOLLOW_UP
),
mandatory
=
1
)
element
.
set
(
Alternative
(
Series
(
Option
(
retrieveop
),
symbol
,
NegativeLookahead
(
Retrieve
(
DEF
))),
literal
,
plaintext
,
regexp
,
char_range
,
Series
(
character
,
dwsp__
),
any_char
,
whitespace
,
group
))
countable
.
set
(
Alternative
(
option
,
oneormore
,
element
))
...
...
@@ -164,9 +164,9 @@ class EBNFGrammar(Grammar):
root__
=
syntax
_raw_grammar
=
ThreadLocalSingletonFactory
(
EBNFGrammar
,
ident
=
1
)
_raw_grammar
=
ThreadLocalSingletonFactory
(
Flexible
EBNFGrammar
,
ident
=
1
)
def
get_grammar
()
->
EBNFGrammar
:
def
get_grammar
()
->
Flexible
EBNFGrammar
:
grammar
=
_raw_grammar
()
if
get_config_value
(
'resume_notices'
):
resume_notices_on
(
grammar
)
...
...
@@ -179,7 +179,7 @@ def get_grammar() -> EBNFGrammar:
pass
return
grammar
def
parse_EBNF
(
document
,
start_parser
=
"root_parser__"
,
*
,
complete_match
=
True
):
def
parse_
Flexible
EBNF
(
document
,
start_parser
=
"root_parser__"
,
*
,
complete_match
=
True
):
return
get_grammar
()(
document
,
start_parser
,
complete_match
)
...
...
examples/FlexibleEBNF/tests_grammar/09_test_top-level.ini
View file @
65dada02
[match:syntax]
M1:
"""
# Hierarchical syntax
Grammar
<-
Spacing
Definition+
EndOfFile
Definition
<-
Identifier
LEFTARROW
Expression
Expression
<-
Sequence
(SLASH
Sequence)*
Sequence
<-
Prefix*
Prefix
<-
(AND
/
NOT)?
Suffix
Suffix
<-
Primary
(QUESTION
/
STAR
/
PLUS)?
Primary
<-
Identifier
!LEFTARROW
/
OPEN
Expression
CLOSE
/
Literal
/
Class
/
DOT
# Lexical syntax
Identifier
<-
IdentStart
IdentCont*
Spacing
IdentStart
<-
[a-zA-Z_]
IdentCont
<-
IdentStart
/
[0-9]
Literal
<-
[´]
(!
[´]
Char)*
[´]
Spacing
/
["]
(!
["]
Char)*
["]
Spacing
Class
<-
´
[´ (!´]
´
Range)*
´]´
Spacing
Range
<-
Char
´-´
Char
/
Char
Char
<-
´\\´
[nrt´"\[\]
\\]
/
´\\´
[0-2][0-7][0-7]
/
´\\´
[0-7][0-7]
?
/
!´\\´
.
LEFTARROW
<-
´<-´
Spacing
SLASH
<-
´/´
Spacing
AND
<-
´&´
Spacing
NOT
<-
´!´
Spacing
QUESTION
<-
´?´
Spacing
STAR
<-
´*´
Spacing
PLUS
<-
´+´
Spacing
OPEN
<-
´(´
Spacing
CLOSE
<-
´)´
Spacing
DOT
<-
´.´
Spacing
Spacing
<-
(Space
/
Comment)*
Comment
<-
´
#´ (!EndOfLine .)* EndOfLine
Space
<-
´
´
/
´\t´
/
EndOfLine
EndOfLine
<-
´\r\n´
/
´\n´
/
´\r´
EndOfFile
<-
!."""
M2:
"""/*
Source:
https://www.w3.org/TR/xml/
*/
document
::=
prolog
element
Misc*
Char
::=
#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
S
::=
(
#x20 | #x9 | #xD | #xA)+
NameStartChar
::=
":"
|
[A-Z]
|
"_"
|
[a-z]
|
[#xC0-#xD6]
|
[#xD8-#xF6]
|
[#xF8-#x2FF]
|
[#x370-#x37D]
|
[#x37F-#x1FFF]
|
[#x200C-#x200D]
|
[#x2070-#x218F]
|
[#x2C00-#x2FEF]
|
[#x3001-#xD7FF]
|
[#xF900-#xFDCF]
|
[#xFDF0-#xFFFD]
|
[#x10000-#xEFFFF]
NameChar
::=
NameStartChar
|
"-"
|
"."
|
[0-9]
|
#xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name
::=
NameStartChar
(NameChar)*
Names
::=
Name
(
#x20 Name)*
Nmtoken
::=
(NameChar)+
Nmtokens
::=
Nmtoken
(
#x20 Nmtoken)*
EntityValue
::=
'"'
(
[^%&"]
|
PEReference
|
Reference)*
'"'
|
"'"
(
[^%&']
|
PEReference
|
Reference)*
"'"
AttValue
::=
'"'
(
[^<&"]
|
Reference)*
'"'
|
"'"
(
[^<&']
|
Reference)*
"'"
SystemLiteral
::=
('"'
[^"]
*
'"')
|
("'"
[^']
*
"'")
PubidLiteral
::=
'"'
PubidChar*
'"'
|
"'"
(PubidChar
-
"'")*
"'"
PubidChar
::=
#x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
CharData
::=
[^<&]
*
-
(
[^<&]
*
']]>'
[^<&]
*)
Comment
::=
'<!--'
((Char
-
'-')
|
('-'
(Char
-
'-')))*
'-->'
PI
::=
'<?'
PITarget
(S
(Char*
-
(Char*
'?>'
Char*)))?
'?>'
PITarget
::=
Name
-
(('X'
|
'x')
('M'
|
'm')
('L'
|
'l'))
CDSect
::=
CDStart
CData
CDEnd
CDStart
::=
'<![CDATA['
CData
::=
(Char*
-
(Char*
']]>'
Char*))
CDEnd
::=
']]>'
prolog
::=
XMLDecl?
Misc*
(doctypedecl
Misc*)?
XMLDecl
::=
'<?xml'
VersionInfo
EncodingDecl?
SDDecl?
S?
'?>'
VersionInfo
::=
S
'version'
Eq
("'"
VersionNum
"'"
|
'"'
VersionNum
'"')
Eq
::=
S?
'='
S?
VersionNum
::=
'1.'
[0-9]
+
Misc
::=
Comment
|
PI
|
S
doctypedecl
::=
'<!DOCTYPE'
S
Name
(S
ExternalID)?
S?
('
[' intSubset ']
'
S?)?
'>'
DeclSep
::=
PEReference
|
S
intSubset
::=
(markupdecl
|
DeclSep)*
markupdecl
::=
elementdecl
|
AttlistDecl
|
EntityDecl
|
NotationDecl
|
PI
|
Comment
extSubset
::=
TextDecl?
extSubsetDecl
extSubsetDecl
::=
(
markupdecl
|
conditionalSect
|
DeclSep)*
SDDecl
::=
S
'standalone'
Eq
(("'"
('yes'
|
'no')
"'")
|
('"'
('yes'
|
'no')
'"'))
element
::=
EmptyElemTag
|
STag
content
ETag
STag
::=
'<'
Name
(S
Attribute)*
S?
'>'
Attribute
::=
Name
Eq
AttValue
ETag
::=
'</'
Name
S?
'>'
content
::=
CharData?
((element
|
Reference
|
CDSect
|
PI
|
Comment)
CharData?)*
EmptyElemTag
::=
'<'
Name
(S
Attribute)*
S?
'/>'
elementdecl
::=
'<!ELEMENT'
S
Name
S
contentspec
S?
'>'
contentspec
::=
'EMPTY'
|
'ANY'
|
Mixed
|
children
children
::=
(choice
|
seq)
('?'
|
'*'
|
'+')?
cp
::=
(Name
|
choice
|
seq)
('?'
|
'*'
|
'+')?
choice
::=
'('
S?
cp
(
S?
'|'
S?
cp
)+
S?
')'
seq
::=
'('
S?
cp
(
S?
','
S?
cp
)*
S?
')'
Mixed
::=
'('
S?
'
#PCDATA' (S? '|' S? Name)* S? ')*'
|
'('
S?
'
#PCDATA' S? ')'
AttlistDecl
::=
'<!ATTLIST'
S
Name
AttDef*
S?
'>'
AttDef
::=
S
Name
S
AttType
S
DefaultDecl
AttType
::=
StringType
|
TokenizedType
|
EnumeratedType
StringType
::=
'CDATA'
TokenizedType
::=
'ID'
|
'IDREF'
|
'IDREFS'
|
'ENTITY'
|
'ENTITIES'
|
'NMTOKEN'
|
'NMTOKENS'
EnumeratedType
::=
NotationType
|
Enumeration
NotationType
::=
'NOTATION'
S
'('
S?
Name
(S?
'|'
S?
Name)*
S?
')'
Enumeration
::=
'('
S?
Nmtoken
(S?
'|'
S?
Nmtoken)*
S?
')'
DefaultDecl
::=
'
#REQUIRED' | '#IMPLIED'
|
(('
#FIXED' S)? AttValue)
conditionalSect
::=
includeSect
|
ignoreSect
includeSect
::=
'<!
[' S? 'INCLUDE' S? '[' extSubsetDecl ']
]>'
ignoreSect
::=
'<!
[' S? 'IGNORE' S? '[' ignoreSectContents* ']
]>'
ignoreSectContents::=
Ignore
('<!
[' ignoreSectContents ']
]>'
Ignore)*
Ignore
::=
Char*
-
(Char*
('<!
[' | ']
]>')
Char*)
CharRef
::=
'&
#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
Reference
::=
EntityRef
|
CharRef
EntityRef
::=
'&'
Name
'
;'
PEReference
::=
'%'
Name
'
;'
EntityDecl
::=
GEDecl
|
PEDecl
GEDecl
::=
'<!ENTITY'
S
Name
S
EntityDef
S?
'>'
PEDecl
::=
'<!ENTITY'
S
'%'
S
Name
S
PEDef
S?
'>'
EntityDef
::=
EntityValue
|
(ExternalID
NDataDecl?)
PEDef
::=
EntityValue
|
ExternalID
ExternalID
::=
'SYSTEM'
S
SystemLiteral
|
'PUBLIC'
S
PubidLiteral
S
SystemLiteral
NDataDecl
::=
S
'NDATA'
S
Name
TextDecl
::=
'<?xml'
VersionInfo?
EncodingDecl
S?
'?>'
extParsedEnt
::=
TextDecl?
content
M1:
"""
# EBNF-Grammar in EBNF
EncodingDecl
::=
S
'encoding'
Eq
('"'
EncName
'"'
|
"'"
EncName
"'"
)
EncName
::=
[A-Za-z]
(
[A-Za-z0-9._]
|
'-')*
/*
Encoding
name
contains
only
Latin
characters
*/
@
comment
=
/#.*(?:
\n
|$)/ # comments start with '#' and eat all chars up to and including '
\n
'
@
whitespace
=
/
\s
*/ # whitespace includes linefeed
@
literalws
=
right # trailing whitespace of literals will be ignored tacitly
@
drop
=
whitespace # do not include whitespace in concrete syntax tree
NotationDecl
::=
'<!NOTATION'
S
Name
S
(ExternalID
|
PublicID)
S?
'>'
[VC: Unique Notation Name]
PublicID
::=
'PUBLIC'
S
PubidLiteral"""
#: top-level
syntax
=
[~//] { definition | directive } §EOF
definition
=
symbol §"=" expression
directive
=
"@"
§symbol "=" (regexp | literal | symbol) { "," (regexp | literal | symbol) }
#: components
expression
=
sequence { "|" sequence }
sequence
=
{ ["§"] term }+ # "§" means all following terms mandatory
term
=
[flowmarker] [retrieveop] symbol !"=" # negative lookahead to be sure it's not a definition
|
[flowmarker]
literal
|
[flowmarker]
plaintext
|
[flowmarker]
regexp
|
[flowmarker]
whitespace
|
[flowmarker]
oneormore
|
[flowmarker]
group
|
[flowmarker]
unordered
|
repetition
|
option
#: flow-operators
flowmarker
=
"!"
| "&" # '!' negative lookahead, '&' positive lookahead
|
"-!"
|
"-&"
# '-' negative lookbehind, '-&' positive lookbehind
retrieveop
=
"::"
| ":?" | ":" # '::' pop, ':?' optional pop, ':' retrieve
#: groups
group
=
"("
§expression ")"
unordered
=
"<"
§expression ">" # elements of expression in arbitrary order
oneormore
=
"{"
expression "}+"
repetition
=
"{"
§expression "}"
option
=
"["
§expression "]"
#: leaf-elements
symbol
=
/(?!
\d
)
\w
+/~ # e.g. expression, term, parameter_list
literal
=
/"(?:(?<!
\\
)
\\
"|[^"])*?"/~ # e.g. "(", '+', 'while'
|
/'(?:(?<!\\)\\'|
[^']
)*?'/~
# whitespace following literals will be ignored tacitly.
plaintext
=
/`(?:(?<!
\\
)
\\
`|[^`])*?`/~ # like literal but does not eat whitespace
regexp
=
/
\/
(?:(?<!
\\
)
\\
(?:
\/
)|[^
\/
])*?
\/
/~ # e.g. /
\w
+/, ~/#.*(?:
\n
|$)/~
whitespace
=
/~/~ # insignificant whitespace
EOF
=
!/./"""
[ast:syntax]
...
...
@@ -182,6 +72,13 @@ M2: """/* Source: https://www.w3.org/TR/xml/ */
[fail:directive]
[match:component]
[ast:component]
[fail:component]
[match:literals]
[ast:literals]
...
...
examples/FlexibleEBNF/tests_grammar/09_test_top-level.ini.disabled
0 → 100644
View file @
65dada02
[match:syntax]
M1: """# Hierarchical syntax
Grammar <- Spacing Definition+ EndOfFile
Definition <- Identifier LEFTARROW Expression
Expression <- Sequence (SLASH Sequence)*
Sequence <- Prefix*
Prefix <- (AND / NOT)? Suffix
Suffix <- Primary (QUESTION / STAR / PLUS)?
Primary <- Identifier !LEFTARROW
/ OPEN Expression CLOSE
/ Literal / Class / DOT
# Lexical syntax
Identifier <- IdentStart IdentCont* Spacing
IdentStart <- [a-zA-Z_]
IdentCont <- IdentStart / [0-9]
Literal <- [´] (![´] Char)* [´] Spacing
/ ["] (!["] Char)* ["] Spacing
Class <- ´[´ (!´]´ Range)* ´]´ Spacing
Range <- Char ´-´ Char / Char
Char <- ´\\´ [nrt´"\[\]\\]
/ ´\\´ [0-2][0-7][0-7]
/ ´\\´ [0-7][0-7]?
/ !´\\´ .
LEFTARROW <- ´<-´ Spacing
SLASH <- ´/´ Spacing
AND <- ´&´ Spacing
NOT <- ´!´ Spacing
QUESTION <- ´?´ Spacing
STAR <- ´*´ Spacing
PLUS <- ´+´ Spacing
OPEN <- ´(´ Spacing
CLOSE <- ´)´ Spacing
DOT <- ´.´ Spacing
Spacing <- (Space / Comment)*
Comment <- ´#´ (!EndOfLine .)* EndOfLine
Space <- ´ ´ / ´\t´ / EndOfLine
EndOfLine <- ´\r\n´ / ´\n´ / ´\r´
EndOfFile <- !."""
M2: """/* Source: https://www.w3.org/TR/xml/ */
document ::= prolog element Misc*
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
S ::= (#x20 | #x9 | #xD | #xA)+
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF]
| [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F]
| [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD]
| [#x10000-#xEFFFF]
NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name ::= NameStartChar (NameChar)*
Names ::= Name (#x20 Name)*
Nmtoken ::= (NameChar)+
Nmtokens ::= Nmtoken (#x20 Nmtoken)*
EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"'
| "'" ([^%&'] | PEReference | Reference)* "'"
AttValue ::= '"' ([^<&"] | Reference)* '"'
| "'" ([^<&'] | Reference)* "'"
SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
CDSect ::= CDStart CData CDEnd
CDStart ::= '<![CDATA['
CData ::= (Char* - (Char* ']]>' Char*))
CDEnd ::= ']]>'
prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
Eq ::= S? '=' S?
VersionNum ::= '1.' [0-9]+
Misc ::= Comment | PI | S
doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
DeclSep ::= PEReference | S
intSubset ::= (markupdecl | DeclSep)*
markupdecl ::= elementdecl | AttlistDecl | EntityDecl | NotationDecl | PI | Comment
extSubset ::= TextDecl? extSubsetDecl
extSubsetDecl ::= ( markupdecl | conditionalSect | DeclSep)*
SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no') '"'))
element ::= EmptyElemTag | STag content ETag
STag ::= '<' Name (S Attribute)* S? '>'
Attribute ::= Name Eq AttValue
ETag ::= '</' Name S? '>'
content ::= CharData? ((element | Reference | CDSect | PI | Comment) CharData?)*
EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
children ::= (choice | seq) ('?' | '*' | '+')?
cp ::= (Name | choice | seq) ('?' | '*' | '+')?
choice ::= '(' S? cp ( S? '|' S? cp )+ S? ')'
seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*'
| '(' S? '#PCDATA' S? ')'
AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
AttDef ::= S Name S AttType S DefaultDecl
AttType ::= StringType | TokenizedType | EnumeratedType
StringType ::= 'CDATA'
TokenizedType ::= 'ID'
| 'IDREF'
| 'IDREFS'
| 'ENTITY'
| 'ENTITIES'
| 'NMTOKEN'
| 'NMTOKENS'
EnumeratedType ::= NotationType | Enumeration
NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
DefaultDecl ::= '#REQUIRED' | '#IMPLIED'
| (('#FIXED' S)? AttValue)
conditionalSect ::= includeSect | ignoreSect
includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
ignoreSectContents::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
Reference ::= EntityRef | CharRef
EntityRef ::= '&' Name ';'
PEReference ::= '%' Name ';'
EntityDecl ::= GEDecl | PEDecl
GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
EntityDef ::= EntityValue | (ExternalID NDataDecl?)
PEDef ::= EntityValue | ExternalID
ExternalID ::= 'SYSTEM' S SystemLiteral | 'PUBLIC' S PubidLiteral S SystemLiteral
NDataDecl ::= S 'NDATA' S Name
TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
extParsedEnt ::= TextDecl? content
EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )
EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* /* Encoding name contains only Latin characters */
NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' [VC: Unique Notation Name]
PublicID ::= 'PUBLIC' S PubidLiteral"""
[ast:syntax]
[fail:syntax]
[match:definition]
[ast:definition]
[fail:definition]
[match:directive]
[ast:directive]
[fail:directive]
[match:literals]
[ast:literals]
[fail:literals]
[match:procedure]
[ast:procedure]
[fail:procedure]
[match:FOLLOW_UP]
[ast:FOLLOW_UP]
[fail:FOLLOW_UP]
examples/FlexibleEBNF/tst_FlexibleEBNF_grammar.py
View file @
65dada02
...
...
@@ -16,7 +16,8 @@ if dhparserdir not in sys.path:
sys
.
path
.
append
(
dhparserdir
)
try
:
from
DHParser.configuration
import
get_config_value
,
set_config_value
from
DHParser.configuration
import
get_config_value
,
set_config_value
,
\
access_presets
,
set_preset_value
,
finalize_presets
from
DHParser
import
dsl
import
DHParser.log
from
DHParser
import
testing
...
...
@@ -57,9 +58,32 @@ def run_grammar_tests(glob_pattern, get_grammar, get_transformer):
return
error_report
def
cpu_profile
(
func
):
import
cProfile
as
profile
import
pstats
pr
=
profile
.
Profile
()
pr
.
enable
()
result
=
func
()
pr
.
disable
()
st
=
pstats
.
Stats
(
pr
)
st
.
strip_dirs
()
st
.
sort_stats
(
'time'
).
print_stats
(
40
)
return
result
if
__name__
==
'__main__'
:
argv
=
sys
.
argv
[:]
if
len
(
argv
)
>
1
and
sys
.
argv
[
1
]
==
"--debug"
:
try
:
i
=
argv
.
index
(
'--profile'
)
del
argv
[
i
]
access_presets
()
set_preset_value
(
'test_parallelization'
,
False
)
finalize_presets
()
print
(
"Profiling test run..."
)
profile
=
True
except
ValueError
:
profile
=
False
if
len
(
argv
)
>
1
and
argv
[
1
]
==
"--debug"
:
LOGGING
=
True
del
argv
[
1
]
if
(
len
(
argv
)
>=
2
and
(
argv
[
1
].
endswith
(
'.ebnf'
)
or
...
...
@@ -77,7 +101,11 @@ if __name__ == '__main__':
force
=
False
)
sys
.
path
.
append
(
'.'
)
from
FlexibleEBNFParser
import
get_grammar
,
get_transformer
error_report
=
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
)
if
profile
:
error_report
=
cpu_profile
(
lambda
:
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
))
else
:
error_report
=
run_grammar_tests
(
arg
,
get_grammar
,
get_transformer
)
if
error_report
:
print
(
'
\n
'
)
print
(
error_report
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment