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
465f90f6
Commit
465f90f6
authored
Nov 02, 2018
by
eckhart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- added syntaxtree.Node.__deepcopy__
parent
ad1709b9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
10 deletions
+24
-10
DHParser/compile.py
DHParser/compile.py
+10
-4
DHParser/ebnf.py
DHParser/ebnf.py
+0
-4
DHParser/parse.py
DHParser/parse.py
+2
-2
DHParser/syntaxtree.py
DHParser/syntaxtree.py
+12
-0
No files found.
DHParser/compile.py
View file @
465f90f6
...
...
@@ -34,6 +34,7 @@ See module ``ebnf`` for a sample of the implementation of a
compiler object.
"""
import
copy
import
os
import
re
...
...
@@ -216,8 +217,9 @@ class Compiler:
def
compile_source
(
source
:
str
,
preprocessor
:
Optional
[
PreprocessorFunc
],
# str -> str
parser
:
Grammar
,
# str -> Node (concrete syntax tree (CST))
transformer
:
TransformationFunc
,
# Node -> Node (abstract syntax tree (AST))
compiler
:
Compiler
)
->
Tuple
[
Any
,
List
[
Error
],
Node
]:
# Node (AST) -> Any
transformer
:
TransformationFunc
,
# Node (CST) -> Node (abstract syntax tree (AST))
compiler
:
Compiler
,
# Node (AST) -> Any
preserve_ast
:
bool
=
False
)
->
Tuple
[
Any
,
List
[
Error
],
Node
]:
"""
Compiles a source in four stages:
1. Pre-Processing (if needed)
...
...
@@ -239,13 +241,15 @@ def compile_source(source: str,
transforms it (in place) into an abstract syntax tree.
compiler (function): A compiler function or compiler class
instance
preserve_ast (bool): Preserves the AST-tree.
Returns (tuple):
The result of the compilation as a 3-tuple
(result, errors, abstract syntax tree). In detail:
1. The result as returned by the compiler or ``None`` in case of failure
2. A list of error or warning messages
3. The root-node of the abstract syntax tree
3. The root-node of the abstract syntax tree if `preserve_ast` is True
or `None` otherwise.
"""
original_text
=
load_if_file
(
source
)
log_file_name
=
logfile_basename
(
source
,
compiler
)
...
...
@@ -272,6 +276,8 @@ def compile_source(source: str,
if
is_logging
():
log_ST
(
syntax_tree
,
log_file_name
+
'.ast'
)
if
not
is_error
(
syntax_tree
.
error_flag
):
if
preserve_ast
:
ast
=
copy
.
deepcopy
(
syntax_tree
)
result
=
compiler
(
syntax_tree
)
# print(syntax_tree.as_sxpr())
# messages.extend(syntax_tree.collect_errors())
...
...
@@ -279,4 +285,4 @@ def compile_source(source: str,
messages
=
syntax_tree
.
collect_errors
()
adjust_error_locations
(
messages
,
original_text
,
source_mapping
)
return
result
,
messages
,
syntax_tree
return
result
,
messages
,
(
ast
if
preserve_ast
else
None
)
DHParser/ebnf.py
View file @
465f90f6
...
...
@@ -540,10 +540,6 @@ class EBNFCompiler(Compiler):
definitions
.
append
((
self
.
WHITESPACE_PARSER_KEYWORD
,
'Whitespace(%s)'
%
self
.
WHITESPACE_KEYWORD
))
# definitions.append(('wspR__', self.WHITESPACE_KEYWORD
# if 'right' in self.directives['literalws'] else "''"))
# definitions.append(('wspL__', self.WHITESPACE_KEYWORD
# if 'left' in self.directives['literalws'] else "''"))
definitions
.
append
((
self
.
WHITESPACE_KEYWORD
,
(
"mixin_comment(whitespace="
+
self
.
RAW_WS_KEYWORD
+
", comment="
+
self
.
COMMENT_KEYWORD
+
")"
)))
...
...
DHParser/parse.py
View file @
465f90f6
...
...
@@ -53,8 +53,8 @@ __all__ = ('Parser',
'TKN'
,
'Whitespace'
,
'mixin_comment'
,
#
'UnaryOperator',
#
'NaryOperator',
'UnaryOperator'
,
'NaryOperator'
,
'Synonym'
,
'Option'
,
'ZeroOrMore'
,
...
...
DHParser/syntaxtree.py
View file @
465f90f6
...
...
@@ -26,6 +26,7 @@ parser classes are defined in the ``parse`` module.
import
collections.abc
from
collections
import
OrderedDict
import
copy
from
DHParser.error
import
Error
,
linebreaks
,
line_col
from
DHParser.stringview
import
StringView
...
...
@@ -278,6 +279,17 @@ class Node(collections.abc.Sized):
self
.
result
=
result
self
.
parser
=
parser
or
ZOMBIE_PARSER
def
__deepcopy__
(
self
,
memo
):
if
self
.
children
:
duplicate
=
self
.
__class__
(
self
.
parser
,
copy
.
deepcopy
(
self
.
children
),
False
)
else
:
duplicate
=
self
.
__class__
(
self
.
parser
,
self
.
result
,
True
)
duplicate
.
errors
=
copy
.
deepcopy
(
self
.
errors
)
if
self
.
errors
else
[]
duplicate
.
_pos
=
self
.
_pos
duplicate
.
_len
=
self
.
_len
if
hasattr
(
self
,
'_xml_attr'
):
duplicate
.
_xml_attr
=
copy
.
deepcopy
(
self
.
_xml_attr
)
return
duplicate
def
__str__
(
self
):
s
=
""
.
join
(
str
(
child
)
for
child
in
self
.
children
)
if
self
.
children
else
self
.
content
...
...
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