57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
|
"""
|
||
|
This module provides the parsing context classes.
|
||
|
"""
|
||
|
from ..util import CharacterDevice
|
||
|
|
||
|
class ParsingContext(object):
|
||
|
def __init__(self, file_, static_context):
|
||
|
self._file = CharacterDevice(file_)
|
||
|
self._static = static_context
|
||
|
self._line = 0
|
||
|
self._column = 0
|
||
|
self._last_column = 0
|
||
|
|
||
|
def getc(self):
|
||
|
res = self._file.getc()
|
||
|
if(res == "\n"):
|
||
|
self._line += 1
|
||
|
self._last_column = self._column
|
||
|
self._column = 0
|
||
|
else:
|
||
|
self._column += 1
|
||
|
return res
|
||
|
def ungetc(self, c):
|
||
|
if(c == "\n"):
|
||
|
self._column = self._last_column
|
||
|
self._line -= 1
|
||
|
else:
|
||
|
self._column -= 1
|
||
|
return self._file.ungetc(c)
|
||
|
def ungets(self, s):
|
||
|
for c in s:
|
||
|
self.ungetc(c)
|
||
|
|
||
|
|
||
|
class StaticParsingContext(object):
|
||
|
def __init__(self, custom_kwd_code_segments, custom_ctx_code_segments):
|
||
|
self._kwd_code_segments = {
|
||
|
(["int", "str", "chr"], ["statement"]): "variable_definition_segment",
|
||
|
(["STOP"], ["statement"]): "stop_command_segment",
|
||
|
(["CALL"], ["statement"]): "routine_call_segment",
|
||
|
(["("], ["expression", "int"]): "arithmetic_segment",
|
||
|
(["ROUTINE"], ["statement"]): "routine_segment",
|
||
|
(["WHILE"], ["statement"]): "while_segment",
|
||
|
(["IF"], ["statement"]): "if_segment",
|
||
|
(["\"", "`"], ["expression", "str"]): "string_definition_segment",
|
||
|
(["'"], ["expression", "chr"]): "character_definition_segment"
|
||
|
|
||
|
}
|
||
|
|
||
|
self._kwd_code_segments.update(custom_kwd_code_segments)
|
||
|
|
||
|
self._ctx_code_segments = {
|
||
|
["statement"]: "assignment_segment",
|
||
|
["expression", "int"]: "arithmetic_segment",
|
||
|
|
||
|
|