Skip to content

Commit

Permalink
Skip multiline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raohmaru committed May 10, 2019
1 parent 3798e4b commit 06cf6fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
30 changes: 22 additions & 8 deletions parser/pasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import os, os.path
import re

__author__ = "Thomas Neubert"
__version__ = "1.0.0"
__author__ = "Thomas Neubert, Raohmaru"
__version__ = "1.1.0"

class Statement:
"""
Expand Down Expand Up @@ -462,8 +462,8 @@ def skip_spaces(line_str, line_pos):
"""
return read(line_str, line_pos, '[\s]')[1]


def skip_comment(line_str, line_pos):
def skip_comment(line_str, line_pos, line_num):
"""
Skip all comments.
Expand All @@ -475,9 +475,10 @@ def skip_comment(line_str, line_pos):
PasmSyntaxError: If comment doesn't start with '//'.
"""
length = len(line_str)
token = line_str[line_pos:line_pos+2]

if (line_pos+1) >= length or \
line_str[line_pos:line_pos+2] != '//':
token != '//' and token != '/*':
msg = _text['error_comment'].format(line_num, line_pos)
raise PasmSyntaxError(msg, line_str, line_num, line_pos)

Expand All @@ -498,6 +499,9 @@ def main(argv=[]):
# input and output file names
f_name_in = None
f_name_out = None

# indicates that the line might be inisde a multiline comment (/* ... */)
multiline_comment = False

# read options
opt_list, args = getopt.getopt(argv, 'qf')
Expand Down Expand Up @@ -537,10 +541,20 @@ def main(argv=[]):
line_pos = skip_spaces(line_str, line_pos)
if line_pos >= length:
continue

# handles multiline comment
if multiline_comment:
if line_str[line_pos:line_pos+2] == '*/':
multiline_comment = False;
continue

# discard comments
if line_str[line_pos] == '/':
skip_comment(line_str, line_pos)
skip_comment(line_str, line_pos, line_num)

# starts multiline comment?
if line_str[line_pos:line_pos+2] == '/*':
multiline_comment = True
continue

# check if there is a label definition
Expand Down Expand Up @@ -616,7 +630,7 @@ def main(argv=[]):

# discard comments
if line_pos < length and line_str[line_pos] == '/':
skip_comment(line_str, line_pos)
skip_comment(line_str, line_pos, line_num)

except PasmSyntaxError as pse:
pse.print_error()
Expand Down Expand Up @@ -675,7 +689,7 @@ def main(argv=[]):
"Error line {}, position {}: Opcode '{}' doesn't exist.",

'error_comment':
"Error line {}, position {}: Start comments with '//'.",
"Error line {}, position {}: Start comments with '//' or '/*'.",

'error_register_1':
"Error line {}, position {}: Invalid register format '{}'. Needs to be\nR<number> where number is 0-255.",
Expand Down
28 changes: 21 additions & 7 deletions parser/pasm_2.7.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import re
import io

__author__ = "Thomas Neubert"
__version__ = "1.0.0"
__author__ = "Thomas Neubert, Raohmaru"
__version__ = "1.1.0"

class Statement:
"""
Expand Down Expand Up @@ -465,7 +465,7 @@ def skip_spaces(line_str, line_pos):
return read(line_str, line_pos, '[\s]')[1]


def skip_comment(line_str, line_pos):
def skip_comment(line_str, line_pos, line_num):
"""
Skip all comments.
Expand All @@ -477,9 +477,10 @@ def skip_comment(line_str, line_pos):
PasmSyntaxError: If comment doesn't start with '//'.
"""
length = len(line_str)
token = line_str[line_pos:line_pos+2]

if (line_pos+1) >= length or \
line_str[line_pos:line_pos+2] != '//':
token != '//' and token != '/*':
msg = _text['error_comment'].format(line_num, line_pos)
raise PasmSyntaxError(msg, line_str, line_num, line_pos)

Expand All @@ -500,6 +501,9 @@ def main(argv=[]):
# input and output file names
f_name_in = None
f_name_out = None

# indicates that the line might be inisde a multiline comment (/* ... */)
multiline_comment = False

# read options
opt_list, args = getopt.getopt(argv, 'qf')
Expand Down Expand Up @@ -539,10 +543,20 @@ def main(argv=[]):
line_pos = skip_spaces(line_str, line_pos)
if line_pos >= length:
continue

# handles multiline comment
if multiline_comment:
if line_str[line_pos:line_pos+2] == '*/':
multiline_comment = False;
continue

# discard comments
if line_str[line_pos] == '/':
skip_comment(line_str, line_pos)
skip_comment(line_str, line_pos, line_num)

# starts multiline comment?
if line_str[line_pos:line_pos+2] == '/*':
multiline_comment = True
continue

# check if there is a label definition
Expand Down Expand Up @@ -618,7 +632,7 @@ def main(argv=[]):

# discard comments
if line_pos < length and line_str[line_pos] == '/':
skip_comment(line_str, line_pos)
skip_comment(line_str, line_pos, line_num)

except PasmSyntaxError as pse:
pse.print_error()
Expand Down Expand Up @@ -677,7 +691,7 @@ def main(argv=[]):
"Error line {}, position {}: Opcode '{}' doesn't exist.",

'error_comment':
"Error line {}, position {}: Start comments with '//'.",
"Error line {}, position {}: Start comments with '//' or '/*'.",

'error_register_1':
"Error line {}, position {}: Invalid register format '{}'. Needs to be\nR<number> where number is 0-255.",
Expand Down

0 comments on commit 06cf6fd

Please sign in to comment.