Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
protokollsystem
proto3
Commits
2eb49f03
Commit
2eb49f03
authored
Sep 03, 2016
by
Robin Sonnabend
Browse files
Small changes on the parser
parent
ca7b1cf3
Changes
1
Hide whitespace changes
Inline
Side-by-side
parser.py
View file @
2eb49f03
import
regex
as
re
import
sys
from
collections
import
OrderedDict
class
ParserException
(
Exception
):
name
=
"Parser Exception"
has_explanation
=
False
#explanation = "The source did generally not match the expected protocol syntax."
def
__init__
(
self
,
message
,
linenumber
=
None
):
self
.
message
=
message
self
.
linenumber
=
linenumber
def
__str__
(
self
):
result
=
""
if
self
.
linenumber
is
not
None
:
result
=
"Exception at line {}: {}"
.
format
(
self
.
linenumber
,
self
.
message
)
else
:
result
=
"Exception: {}"
.
format
(
self
.
message
)
if
self
.
has_explanation
:
result
+=
"
\n
"
+
self
.
explanation
return
result
class
Element
:
"""
Generic (abstract) base element. Should never really exist.
...
...
@@ -71,7 +85,7 @@ class Element:
return
element
return
current
PATTERN
=
r
"x(?<!x)"
# yes, a master piece
PATTERN
=
r
"x(?<!x)"
# yes, a master piece
, but it should never be called
class
Content
(
Element
):
def
__init__
(
self
,
children
):
...
...
@@ -115,7 +129,10 @@ class Content(Element):
raise
ParserException
(
"Content does not match inner!"
,
linenumber
)
return
Content
(
children
)
PATTERN
=
r
"\s*(?<content>(?:[^\[\];]+)?(?:\[[^\]]+\][^;\[\]]*)*);"
# v1: has problems with missing semicolons
#PATTERN = r"\s*(?<content>(?:[^\[\];]+)?(?:\[[^\]]+\][^;\[\]]*)*);"
# v2: does not require the semicolon, but the newline
PATTERN
=
r
"\s*(?<content>(?:[^\[\];\r\n]+)?(?:\[[^\]\r\n]+\][^;\[\]\r\n]*)*);?"
class
Text
:
def
__init__
(
self
,
text
):
...
...
@@ -152,7 +169,7 @@ class Tag:
def
dump
(
self
,
level
=
None
):
if
level
is
None
:
level
=
0
return
"{}tag: {}: {}"
.
format
(
" "
*
level
,
self
.
name
,
"; "
.
join
(
self
.
values
))
print
(
"{}tag: {}: {}"
.
format
(
" "
*
level
,
self
.
name
,
"; "
.
join
(
self
.
values
))
)
@
staticmethod
def
parse
(
match
,
linenumber
):
...
...
@@ -301,16 +318,24 @@ def parse(source):
break
if
not
found
:
raise
ParserException
(
"No matching syntax element found!"
,
linenumber
)
if
current
is
not
tree
:
raise
ParserException
(
"Source ended within fork!"
)
return
tree
def
main
():
def
main
(
test_file_name
=
None
):
source
=
""
with
open
(
"test/source0.txt"
)
as
f
:
test_file_name
=
test_file_name
or
"source0"
with
open
(
"test/{}.txt"
.
format
(
test_file_name
))
as
f
:
source
=
f
.
read
()
tree
=
parse
(
source
)
#tree.dump()
print
(
tree
.
render
())
try
:
tree
=
parse
(
source
)
tree
.
dump
()
except
ParserException
as
e
:
print
(
e
)
else
:
print
(
"worked!"
)
if
__name__
==
"__main__"
:
exit
(
main
())
test_file_name
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
>
1
else
None
exit
(
main
(
test_file_name
))
Write
Preview
Supports
Markdown
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