Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
8c25efce
Commit
8c25efce
authored
Aug 08, 2017
by
Eckhart Arnold
Browse files
further LaTeX-tests
parent
7803dedb
Changes
8
Hide whitespace changes
Inline
Side-by-side
DHParser/parser.py
View file @
8c25efce
...
...
@@ -731,11 +731,12 @@ class Grammar:
def
write_log
(
history
,
log_name
):
path
=
os
.
path
.
join
(
log_dir
(),
log_name
+
"_parser.log"
)
if
os
.
path
.
exists
(
path
):
os
.
remove
(
path
)
print
(
'WARNING: Log-file "%s" already existed and was deleted.'
%
path
)
if
history
:
with
open
(
path
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
"
\n
"
.
join
(
history
))
elif
os
.
path
.
exists
(
path
):
os
.
remove
(
path
)
if
is_logging
():
assert
self
.
history__
,
\
...
...
DHParser/syntaxtree.py
View file @
8c25efce
...
...
@@ -497,11 +497,13 @@ class Node:
def
log
(
self
,
log_file_name
):
if
is_logging
():
with
open
(
os
.
path
.
join
(
log_dir
(),
log_file_name
),
"w"
,
encoding
=
"utf-8"
)
as
f
:
path
=
os
.
path
.
join
(
log_dir
(),
log_file_name
)
if
os
.
path
.
exists
(
path
):
print
(
'WARNING: Log-file "%s" already exists and will be overwritten!'
%
path
)
with
open
(
path
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
self
.
as_sxpr
())
def
mock_syntax_tree
(
sxpr
):
"""
Generates a tree of nodes from an S-expression.
...
...
DHParser/testing.py
View file @
8c25efce
...
...
@@ -26,7 +26,7 @@ try:
except
ImportError
:
import
re
from
DHParser
import
error_messages
from
DHParser
.toolkit
import
is_logging
,
clear_logs
,
error_messages
from
DHParser.syntaxtree
import
mock_syntax_tree
,
flatten_sxpr
__all__
=
(
'unit_from_configfile'
,
...
...
@@ -149,13 +149,13 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
infostr
=
' match-test "'
+
test_name
+
'" ... '
errflag
=
len
(
errata
)
cst
=
parser
(
test_code
,
parser_name
)
cst
.
log
(
"
%s_
match_%s_%s.cst"
%
(
unit_name
,
parser_name
,
test_name
))
cst
.
log
(
"match_%s_%s.cst"
%
(
parser_name
,
test_name
))
tests
.
setdefault
(
'__cst__'
,
{})[
test_name
]
=
cst
if
"ast"
in
tests
or
report
:
ast
=
copy
.
deepcopy
(
cst
)
transform
(
ast
)
tests
.
setdefault
(
'__ast__'
,
{})[
test_name
]
=
ast
ast
.
log
(
"
%s_
match_%s_%s.ast"
%
(
unit_name
,
parser_name
,
test_name
))
ast
.
log
(
"match_%s_%s.ast"
%
(
parser_name
,
test_name
))
if
cst
.
error_flag
:
errata
.
append
(
'Match test "%s" for parser "%s" failed:
\n\t
Expr.: %s
\n\n\t
%s
\n\n
'
%
(
test_name
,
parser_name
,
'
\n\t
'
.
join
(
test_code
.
split
(
'
\n
'
)),
...
...
@@ -217,6 +217,7 @@ def grammar_suite(directory, parser_factory, transformer_factory, ignore_unknown
print
(
"
\n
Scanning test-directory: "
+
directory
)
save_cwd
=
os
.
getcwd
()
os
.
chdir
(
directory
)
if
is_logging
():
clear_logs
()
for
filename
in
sorted
(
os
.
listdir
()):
if
filename
.
lower
().
find
(
"test"
)
>=
0
:
try
:
...
...
DHParser/toolkit.py
View file @
8c25efce
...
...
@@ -133,6 +133,23 @@ def is_logging() -> bool:
return
False
def
clear_logs
(
logfile_types
=
{
'.cst'
,
'.ast'
,
'.log'
}):
"""Removes all logs from the log-directory and removes the
log-directory if it is empty.
"""
log_dirname
=
log_dir
()
files
=
os
.
listdir
(
log_dirname
)
only_log_files
=
True
for
file
in
files
:
path
=
os
.
path
.
join
(
log_dirname
,
file
)
if
os
.
path
.
splitext
(
file
)[
1
]
in
logfile_types
or
file
==
'info.txt'
:
os
.
remove
(
path
)
else
:
only_log_files
=
False
if
only_log_files
:
os
.
rmdir
(
log_dirname
)
# def repr_call(f, parameter_list) -> str:
# """Turns a list of items into a string resembling the parameter
# list of a function call by omitting default values at the end:
...
...
examples/LaTeX/LaTeX.ebnf
View file @
8c25efce
...
...
@@ -6,7 +6,7 @@
latexdoc = preamble document
preamble = { command }+
preamble = {
[WSPC]
command }+
document = [WSPC] "\begin{document}" [WSPC]
frontpages [WSPC]
...
...
@@ -23,16 +23,16 @@ frontpages = sequence
#######################################################################
Chapters = { Chapter [WSPC] }+
Chapter = "\
C
hapter" block [WSPC] { sequence | Sections }
Chapter = "\
c
hapter" block [WSPC] { sequence | Sections }
Sections = { Section [WSPC] }+
Section = "\
S
ection" block [WSPC] { sequence | SubSections }
Section = "\
s
ection" block [WSPC] { sequence | SubSections }
SubSections = { SubSection [WSPC] }+
SubSection = "\
S
ub
S
ection" block [WSPC] { sequence | SubSubSections }
SubSection = "\
s
ub
s
ection" block [WSPC] { sequence | SubSubSections }
SubSubSections = { SubSubSection [WSPC] }+
SubSubSection = "\
S
ub
S
ub
S
ection" block [WSPC] { sequence | Paragraphs }
SubSubSection = "\
s
ub
s
ub
s
ection" block [WSPC] { sequence | Paragraphs }
Paragraphs = { Paragraph [WSPC] }+
Paragraph = "\paragraph" block [WSPC] { sequence | SubParagraphs }
...
...
examples/LaTeX/LaTeXCompiler.py
View file @
8c25efce
...
...
@@ -72,16 +72,16 @@ class LaTeXGrammar(Grammar):
#######################################################################
Chapters = { Chapter [WSPC] }+
Chapter = "\
C
hapter" block [WSPC] { sequence | Sections }
Chapter = "\
c
hapter" block [WSPC] { sequence | Sections }
Sections = { Section [WSPC] }+
Section = "\
S
ection" block [WSPC] { sequence | SubSections }
Section = "\
s
ection" block [WSPC] { sequence | SubSections }
SubSections = { SubSection [WSPC] }+
SubSection = "\
S
ub
S
ection" block [WSPC] { sequence | SubSubSections }
SubSection = "\
s
ub
s
ection" block [WSPC] { sequence | SubSubSections }
SubSubSections = { SubSubSection [WSPC] }+
SubSubSection = "\
S
ub
S
ub
S
ection" block [WSPC] { sequence | Paragraphs }
SubSubSection = "\
s
ub
s
ub
s
ection" block [WSPC] { sequence | Paragraphs }
Paragraphs = { Paragraph [WSPC] }+
Paragraph = "\paragraph" block [WSPC] { sequence | SubParagraphs }
...
...
@@ -210,7 +210,7 @@ class LaTeXGrammar(Grammar):
end_generic_block
=
Forward
()
paragraph
=
Forward
()
text_element
=
Forward
()
source_hash__
=
"
529c853d5829c3016605e4ee7ed69ddb
"
source_hash__
=
"
c77c91d639f2898ac2a58e1bfdf7e608
"
parser_initialization__
=
"upon instantiation"
COMMENT__
=
r
'%.*(?:\n|$)'
WSP__
=
mixin_comment
(
whitespace
=
r
'[ \t]*(?:\n(?![ \t]*\n)[ \t]*)?'
,
comment
=
r
'%.*(?:\n|$)'
)
...
...
@@ -274,13 +274,13 @@ class LaTeXGrammar(Grammar):
SubParagraphs
=
OneOrMore
(
Series
(
SubParagraph
,
Optional
(
WSPC
)))
Paragraph
=
Series
(
Token
(
"
\\
paragraph"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
SubParagraphs
)))
Paragraphs
=
OneOrMore
(
Series
(
Paragraph
,
Optional
(
WSPC
)))
SubSubSection
=
Series
(
Token
(
"
\\
S
ub
S
ub
S
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
Paragraphs
)))
SubSubSection
=
Series
(
Token
(
"
\\
s
ub
s
ub
s
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
Paragraphs
)))
SubSubSections
=
OneOrMore
(
Series
(
SubSubSection
,
Optional
(
WSPC
)))
SubSection
=
Series
(
Token
(
"
\\
S
ub
S
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
SubSubSections
)))
SubSection
=
Series
(
Token
(
"
\\
s
ub
s
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
SubSubSections
)))
SubSections
=
OneOrMore
(
Series
(
SubSection
,
Optional
(
WSPC
)))
Section
=
Series
(
Token
(
"
\\
S
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
SubSections
)))
Section
=
Series
(
Token
(
"
\\
s
ection"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
SubSections
)))
Sections
=
OneOrMore
(
Series
(
Section
,
Optional
(
WSPC
)))
Chapter
=
Series
(
Token
(
"
\\
C
hapter"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
Sections
)))
Chapter
=
Series
(
Token
(
"
\\
c
hapter"
),
block
,
Optional
(
WSPC
),
ZeroOrMore
(
Alternative
(
sequence
,
Sections
)))
Chapters
=
OneOrMore
(
Series
(
Chapter
,
Optional
(
WSPC
)))
frontpages
=
Synonym
(
sequence
)
document
=
Series
(
Optional
(
WSPC
),
Token
(
"
\\
begin{document}"
),
Optional
(
WSPC
),
frontpages
,
Optional
(
WSPC
),
Alternative
(
Chapters
,
Sections
),
Optional
(
WSPC
),
Optional
(
Bibliography
),
Optional
(
Index
),
Optional
(
WSPC
),
Token
(
"
\\
end{document}"
),
Optional
(
WSPC
),
Required
(
EOF
))
...
...
examples/LaTeX/grammar_tests/02_test_paragraph.ini
View file @
8c25efce
...
...
@@ -31,6 +31,13 @@
and
\textbackslash
text-commands
or
other
commands
like
this
\footnote{footnote}
10:
"""\begin{generic}inline
environment\end{generic}
"""
11:
"""\begin{generic}inline
environment
\end{generic}
"""
[fail:paragraph]
1
:
\begin{enumerate}
...
...
examples/LaTeX/grammar_tests/03_test_environment.ini
View file @
8c25efce
...
...
@@ -47,14 +47,6 @@
invalid
enivronment
\end{generic}
"""
[match:paragraph]
1
:
"""\begin{generic}inline
environment\end{generic}
"""
2
:
"""\begin{generic}inline
environment
\end{generic}
"""
[match:itemize]
1
:
\begin{itemize}
\item
Items
doe
not
need
to
be
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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