Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
DHParser
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
badw-it
DHParser
Commits
0c242f56
Commit
0c242f56
authored
Aug 25, 2017
by
di68kap
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- dhparser can now create project templates
parent
bd0e6011
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
164 additions
and
49 deletions
+164
-49
dhparser.py
dhparser.py
+130
-43
examples/BibTeX/BibTeX.ebnf
examples/BibTeX/BibTeX.ebnf
+31
-5
examples/BibTeX/tst_BibTeX_grammar.py
examples/BibTeX/tst_BibTeX_grammar.py
+3
-1
No files found.
dhparser.py
View file @
0c242f56
...
...
@@ -22,41 +22,113 @@ permissions and limitations under the License.
import
os
import
sys
from
functools
import
partial
from
DHParser.dsl
import
compileDSL
,
compile_on_disk
from
DHParser.ebnf
import
get_ebnf_grammar
,
get_ebnf_transformer
,
get_ebnf_compiler
from
DHParser.parser
import
compile_source
,
nil_preprocessor
from
DHParser.parser
import
compile_source
from
DHParser.toolkit
import
logging
# def selftest(file_name):
# print(file_name)
# with open('examples/' + file_name, encoding="utf-8") as f:
# grammar = f.read()
# compiler_name = os.path.basename(os.path.splitext(file_name)[0])
# parser = get_ebnf_grammar()
# print("\nAlphabetical List of Parsers:\n")
# parser_list = sorted([p for p in parser.all_parsers__ if p.name], key=lambda p: p.name)
# for p in parser_list:
# print(p)
# print('\n\n')
# transformer = get_ebnf_transformer()
# compiler = get_ebnf_compiler(compiler_name, grammar)
# result, errors, syntax_tree = compile_source(grammar, None, parser,
# transformer, compiler)
# print(result)
# if errors:
# print('\n\n'.merge_children(errors))
# sys.exit(1)
# else:
# # compile the grammar again using the result of the previous
# # compilation as parser
# for i in range(1):
# result = compileDSL(grammar, nil_preprocessor, result, transformer, compiler)
# print(result)
# return result
EBNF_TEMPLATE
=
"""# {name}-grammar
#######################################################################
#
# EBNF-Directives
#
#######################################################################
@ testing = True # testing supresses error messages for unconnected symbols
@ whitespace = horizontal # implicit whitespace ends at the end of the line
@ literalws = right # literals have implicit whitespace on the right hand side
@ comment = /#.*(?:
\n
|$)/ # comments range from a '#'-character to the end of the line
@ ignorecase = False # literals and regular expressions are case-sensitive
#######################################################################
#
# Structure and Components
#
#######################################################################
document = //~ { WORD } §EOF # root parser: optional whitespace followed by a sequence of words
# until the end of file
#######################################################################
#
# Regular Expressions
#
#######################################################################
WORD = /\w+/~ # a sequence of letters, possibly followed by implicit whitespace
EOF = !/./ # no more characters ahead, end of file reached
"""
README_TEMPLATE
=
"""# {name}
PLACE A SHORT DESCRIPTION HERE
Author: AUTHOR'S NAME <EMAIL>, AFFILIATION
## License
{name} is open source software under the [MIT License](https://opensource.org/licenses/MIT)
Copyright YEAR AUTHOR'S NAME <EMAIL>, AFFILIATION
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
GRAMMAR_TEST_TEMPLATE
=
'''#!/usr/bin/python3
"""tst_{name}_grammar.py - runs the unit tests for the {name}-grammar
"""
import sys
import DHParser.dsl
from DHParser import testing
from DHParser import toolkit
if not DHParser.dsl.recompile_grammar('{name}.ebnf', force=False): # recompiles Grammar only if it has changed
print('
\n
Errors while recompiling "{name}.ebnf":
\n
--------------------------------------
\n\n
')
with open('{name}_ebnf_ERRORS.txt') as f:
print(f.read())
sys.exit(1)
sys.path.append('./')
# must be appended after module creation, because otherwise an ImportError is raised under Windows
from {name}Compiler import get_grammar, get_transformer
with toolkit.logging(True):
error_report = testing.grammar_suite('grammar_tests', get_grammar,
get_transformer, report=True, verbose=True)
if error_report:
print('
\n
')
print(error_report)
sys.exit(1)
else:
print('
\n
SUCCESS! All tests passed :-)')
'''
def
selftest
()
->
bool
:
...
...
@@ -83,6 +155,28 @@ def selftest() -> bool:
return
True
def
create_project
(
path
,
ebnf_tmpl
=
EBNF_TEMPLATE
,
readme_tmpl
=
README_TEMPLATE
,
grammar_test_tmpl
=
GRAMMAR_TEST_TEMPLATE
):
if
os
.
_exists
(
path
):
print
(
'Cannot create new DHParser-project, because path %s alread exists!'
%
path
)
sys
.
exit
(
1
)
name
=
os
.
path
.
basename
(
path
)
print
(
'Creating new DHParser-project "%s"...'
%
name
)
os
.
mkdir
(
path
)
# curr_dir = os.getcwd()
os
.
chdir
(
path
)
os
.
mkdir
(
'grammar_tests'
)
with
open
(
name
+
'.ebnf'
,
'w'
)
as
f
:
f
.
write
(
EBNF_TEMPLATE
.
format
(
name
=
name
))
with
open
(
'README.md'
,
'w'
)
as
f
:
f
.
write
(
README_TEMPLATE
.
format
(
name
=
name
))
with
open
(
'tst_%s_grammar.py'
)
as
f
:
f
.
write
(
GRAMMAR_TEST_TEMPLATE
.
format
(
name
=
name
))
# os.chdir(curr_dir)
print
(
'ready.'
)
def
profile
(
func
):
import
cProfile
,
pstats
...
...
@@ -97,26 +191,19 @@ def profile(func):
st
=
pstats
.
Stats
(
pr
)
st
.
strip_dirs
()
st
.
sort_stats
(
'time'
).
print_stats
(
10
)
# pr.print_stats(sort="tottime")
return
success
# # Changes in the EBNF source that are not reflected in this file could be
# # a source of sometimes obscure errors! Therefore, we will check this.
# if (os.path.exists('examples/EBNF/EBNF.ebnf')
# and source_changed('examples/EBNF/EBNF.ebnf', EBNFGrammar)):
# assert False, "WARNING: Grammar source has changed. The parser may not " \
# "represent the actual grammar any more!!!"
# pass
if
__name__
==
"__main__"
:
print
(
sys
.
argv
)
if
len
(
sys
.
argv
)
>
1
:
_errors
=
compile_on_disk
(
sys
.
argv
[
1
],
sys
.
argv
[
2
]
if
len
(
sys
.
argv
)
>
2
else
""
)
if
_errors
:
print
(
'
\n\n
'
.
join
(
_errors
))
sys
.
exit
(
1
)
if
os
.
path
.
exists
(
sys
.
argv
[
1
]):
_errors
=
compile_on_disk
(
sys
.
argv
[
1
],
sys
.
argv
[
2
]
if
len
(
sys
.
argv
)
>
2
else
""
)
if
_errors
:
print
(
'
\n\n
'
.
join
(
_errors
))
sys
.
exit
(
1
)
else
:
create_project
(
sys
.
argv
[
1
])
else
:
# run self test
# selftest('EBNF/EBNF.ebnf')
...
...
examples/BibTeX/BibTeX.ebnf
View file @
0c242f56
# BibTeX-Grammar
#######################################################################
#
# EBNF-Directives
#
######################################################################
@ testing = True
@ whitespace = /\s*/
@ ignorecase = True
@ comment = /%.*(?:\n|$)/
#######################################################################
#
# Bib-file Structure and Components
#
#######################################################################
bibliography = { preamble | comment | entry }
preamble = "@Preamble{" /"/ pre_code /"/~ §"}"
...
...
@@ -13,10 +26,23 @@ pre_code = { /[^"%]+/ | /%.*\n/ }
comment = "@Comment{" text §"}"
entry = /@/ type "{" key { "," field §"=" content } §"}"
type =
/\w+/
key =
/[^ \t\n,%]+/~
field =
/\w+/~
type =
WORD
key =
NO_BLANK_STRING
field =
WORD_
content = "{" text "}" | plain_content
plain_content = { /[^,%]+/ | /(?=%)/~ }
text = { /(?:\\\{|\\\}|[^}%])+/~ | /\{/ text /\}/ | /(?=%)/~ }
plain_content = COMMA_TERMINATED_STRING
text = NESTED_BRACES_STRING
#######################################################################
#
# Regular Expressions
#
#######################################################################
WORD = /\w+/
WORD_ = /\w+/~
NO_BLANK_STRING = /[^ \t\n,%]+/~
COMMA_TERMINATED_STRING = { /[^,%]+/ | /(?=%)/~ }
NESTED_BRACES_STRING = { /(?:\\\{|\\\}|[^}%])+/~ | /\{/ NESTED_BRACES_STRING /\}/ | /(?=%)/~ }
examples/BibTeX/tst_BibTeX_grammar.py
View file @
0c242f56
...
...
@@ -21,7 +21,7 @@ limitations under the License.
import
sys
sys
.
path
.
extend
([
'../../'
,
'../'
,
'./'
])
sys
.
path
.
extend
([
'../../'
,
'../'
])
import
DHParser.dsl
from
DHParser
import
testing
...
...
@@ -33,6 +33,8 @@ if not DHParser.dsl.recompile_grammar('BibTeX.ebnf', force=False): # recompiles
print
(
f
.
read
())
sys
.
exit
(
1
)
sys
.
path
.
append
(
'./'
)
# must be appended after module creation, because otherwise an ImportError is raised under Windows
from
BibTeXCompiler
import
get_grammar
,
get_transformer
with
toolkit
.
logging
(
True
):
...
...
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