Commit 08efa377 authored by Fabrice Salvaire's avatar Fabrice Salvaire

implemented gcode parser

parent 84aa863b
......@@ -18,11 +18,16 @@
#
####################################################################################################
__all__ = ['GCodeLexerError', 'GCodeLexer, ']
####################################################################################################
import re
import PythonicGCodeMachine.Lexer as lexer
try:
import ply.lex as lexer
except ModuleNotFoundError:
import PythonicGCodeMachine.PythonLexYacc.lex as lexer
####################################################################################################
......@@ -50,7 +55,6 @@ class GCodeLexer:
'ARC_SINE',
'ARC_TANGENT',
'COSINE',
'DECIMAL_POINT',
'DIVIDED_BY',
'EQUAL_SIGN',
'EXCLUSIVE_OR',
......@@ -58,7 +62,7 @@ class GCodeLexer:
'FIX_DOWN',
'FIX_UP',
'LEFT_BRACKET',
'LEFT_PARENTHESIS',
# 'LEFT_PARENTHESIS',
'MINUS',
'MODULO',
'NATURAL_LOG_OF',
......@@ -67,7 +71,7 @@ class GCodeLexer:
'PLUS',
'POWER',
'RIGHT_BRACKET',
'RIGHT_PARENTHESIS',
# 'RIGHT_PARENTHESIS',
'ROUND',
'SINE',
'SQUARE_ROOT',
......@@ -92,17 +96,16 @@ class GCodeLexer:
t_ARC_COSINE = r'acos'
t_ARC_SINE = r'asin'
t_ARC_TANGENT = r'atan'
# t_BLOCK_DELETE = r'\/'
# t_BLOCK_DELETE = r'\/' # slash
t_COSINE = r'cos'
t_DECIMAL_POINT = r'\.'
t_DIVIDED_BY = r'\/'
t_DIVIDED_BY = r'\/' # slash
t_EQUAL_SIGN = r'='
t_EXCLUSIVE_OR = r'xor'
t_E_RAISED_TO = r'exp'
t_FIX_DOWN = r'fix'
t_FIX_UP = r'fup'
t_LEFT_BRACKET = r'\['
t_LEFT_PARENTHESIS = r'\('
# t_LEFT_PARENTHESIS = r'\('
t_MINUS = r'-'
t_MODULO = r'mod'
t_NATURAL_LOG_OF = r'ln'
......@@ -111,7 +114,7 @@ class GCodeLexer:
t_PLUS = r'\+'
t_POWER = r'\*\*'
t_RIGHT_BRACKET = r'\]'
t_RIGHT_PARENTHESIS = r'\)'
# t_RIGHT_PARENTHESIS = r'\)'
t_ROUND = r'round'
t_SINE = r'sin'
t_SQUARE_ROOT = r'sqrt'
......@@ -155,7 +158,7 @@ class GCodeLexer:
def t_REAL(self, t):
# r'((-)?((\d*\.\d+)(E[\+-]?\d+)?|([1-9]\d*E[\+-]?\d+)))'
r'((-)?\d*(\.)?\d+)'
r'((\+|-)?(\d+\.\d*|(\.)?\d+))'
value = t.value
if '.' in value:
value = float(value)
......@@ -204,14 +207,20 @@ class GCodeLexer:
self._lexer = lexer.lex(
module=self,
reflags=int(re.VERBOSE + re.IGNORECASE),
optimize=1,
**kwargs,
)
##############################################
def input(self, data):
return self._lexer.input(data)
##############################################
def tokenize(self, data):
self._lexer.input(data)
self.input(data)
while True:
token = self._lexer.token()
if not token:
......
This diff is collapsed.
......@@ -24,7 +24,16 @@ import unittest
####################################################################################################
from PythonicGCodeMachine.GCode.Lexer import GCodeLexer
from PythonicGCodeMachine.GCode.Lexer import GCodeLexer, GCodeLexerError
from PythonicGCodeMachine.GCode.Parser import GCodeParser, GCodeParserError
####################################################################################################
def print_rule():
print()
print('#'*100)
print()
####################################################################################################
......@@ -34,6 +43,8 @@ class TestGCodeLexer(unittest.TestCase):
def test_gcode_lexer(self):
print_rule()
lexer = GCodeLexer()
for gcode in (
......@@ -41,21 +52,80 @@ class TestGCodeLexer(unittest.TestCase):
'g0 x0 y0 z0',
'G0X0Y0Z0',
r'/ G0 X0 Y0 Z0',
'N1 G0 X0 Y0 Z0',
'N2 G0 X1.0 Y0 Z0',
'N3.1 G0 X1.0 Y0 Z0',
'N3.1 G0 X1.0 Y0 Z0 ; a eof comment',
'N3.1 (comment 1) G0 (comment 2) X1.0 (comment 3) Y0 (comment 4) Z0 ; a eof comment',
'#3=1. G0 X [ 1 + acos[0] - [#3 ** [4.0/2]]]'
):
tokens = lexer.tokenize(gcode)
print(gcode, list(tokens))
print()
print(gcode)
try:
tokens = lexer.tokenize(gcode)
print(list(tokens))
except GCodeLexerError as exception:
position, = exception.args
print(' ' * position + '^')
print('Lexer Error')
raise exception
for gcode in (
'G0 (comment (wrong) 2) X0',
):
tokens = lexer.tokenize(gcode)
print(gcode, list(tokens))
with self.assertRaises(GCodeLexerError):
list(lexer.tokenize(gcode))
####################################################################################################
class TestGCodeParser(unittest.TestCase):
##############################################
def test_gcode_parser(self):
print_rule()
parser = GCodeParser()
for gcode in (
'G0 X0 Y0 Z0',
'g0 x0 y0 z0',
'G0X0Y0Z0',
r'/ G0 X0 Y0 Z0',
'N1 G0 X0 Y0 Z0',
'N2 G0 X1.0 Y0 Z0',
'N3.1 G0 X1.0 Y0 Z0',
'N3.1 G0 X1.0 Y0 Z0 ; a eof comment',
'N3.1 (comment 1) G0 (comment 2) X1.0 (comment 3) Y0 (comment 4) Z0 ; a eof comment',
'#3=1. G0 X#3 Y0'
'G0 #3=1. X#3 Y0'
'#3=1. G0 X [ 1 + acos[0] - [#3 ** [4.0/2]]]'
):
print()
print(gcode)
try:
parser.parse(gcode)
except GCodeParserError as exception:
position, = exception.args
print(' ' * position + '^')
print('Parser Error')
raise exception
# for gcode in (
# 'G0 (comment (wrong) 2) X0',
# ):
# with self.assertRaises(GCodeLexerError):
# list(lexer.tokenize(gcode))
####################################################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment