Commit 9a3bbc2c authored by Fabrice Salvaire's avatar Fabrice Salvaire

Implemented AST

parent ce57b38e
This diff is collapsed.
......@@ -18,7 +18,7 @@
#
####################################################################################################
__all__ = ['GcodeLexerError', 'GcodeLexer, ']
__all__ = ['GcodeLexerError', 'GcodeLexer']
####################################################################################################
......@@ -38,7 +38,7 @@ class GcodeLexerError(ValueError):
class GcodeLexer:
"""Class to implement a CGode lexer.
"""Class to implement a RS-274 G-code lexer.
For references, see
......@@ -219,7 +219,6 @@ class GcodeLexer:
##############################################
def tokenize(self, data):
self.input(data)
while True:
token = self._lexer.token()
......
......@@ -25,7 +25,7 @@ __all__ = ['GcodeParserError', 'GcodeParser']
# https://rply.readthedocs.io/en/latest/
from ply import yacc
from .Lexer import GcodeLexer, GcodeLexerError
from .Lexer import GcodeLexer
####################################################################################################
......@@ -36,7 +36,7 @@ class GcodeParserError(ValueError):
class GcodeParser:
"""Class to implement a CGode parser.
"""Class to implement a RS-274 G-code parser.
For references, see
......@@ -91,6 +91,8 @@ class GcodeParser:
"""
__lexer_cls__ = GcodeLexer
##############################################
# Start symbol
......@@ -267,8 +269,7 @@ class GcodeParser:
def _build(self, **kwargs):
"""Build the parser"""
self._lexer = GcodeLexer()
self._lexer = self.__lexer_cls__()
self.tokens = self._lexer.tokens
self._parser = yacc.yacc(
module=self,
......@@ -279,7 +280,6 @@ class GcodeParser:
##############################################
def parse(self, line):
line = line.strip()
ast = self._parser.parse(
line,
......
......@@ -163,7 +163,7 @@ value may be an explicit number (such as 341 or -0.8807), a parameter value, an
unary operation value.
Number
~~~~~
~~~~~~
The following rules are used for (explicit) numbers. In these rules a digit is a single character
between 0 and 9.
......
####################################################################################################
#
# PythonicGcodeMachine - @licence_header_description@
# Copyright (C) 2018 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
####################################################################################################
import unittest
####################################################################################################
from PythonicGcodeMachine.Gcode.Rs274.Ast import *
####################################################################################################
class TestAst(unittest.TestCase):
##############################################
def test_basic(self):
# program = Program()
word = Word('G', 0)
self.assertEqual(str(word), 'G0')
word = Word('g', 0)
self.assertEqual(str(word), 'G0')
word = Word('G', 1.0)
self.assertEqual(str(word), 'G1.0')
line = Line()
line.push(Word('G', 1))
self.assertEqual(str(line), 'G1')
line.comment = 'a comment'
self.assertEqual(str(line), 'G1 ; a comment')
line.line_number = 1
self.assertEqual(str(line), 'N1 G1 ; a comment')
line.line_number = 1.1
self.assertEqual(str(line), 'N1.1 G1 ; a comment')
line.deleted = True
self.assertEqual(str(line), r'\ N1.1 G1 ; a comment')
line = Line()
for value, letter in enumerate('GXYZ'):
line.push(Word(letter, value))
self.assertEqual(str(line), 'G0 X1 Y2 Z3')
line = Line()
for value, letter in enumerate('GXYZ'):
line += Word(letter, value)
self.assertEqual(str(line), 'G0 X1 Y2 Z3')
line.push(ParameterSetting(1, 1.2))
self.assertEqual(str(line), 'G0 X1 Y2 Z3 #1=1.2')
##############################################
def test_expression(self):
self.assertEqual(str(Plus(1, 2)), '[1 + 2]')
####################################################################################################
if __name__ == '__main__':
unittest.main()
......@@ -24,8 +24,8 @@ import unittest
####################################################################################################
from PythonicGcodeMachine.Gcode.Lexer import GcodeLexer, GcodeLexerError
from PythonicGcodeMachine.Gcode.Parser import GcodeParser, GcodeParserError
from PythonicGcodeMachine.Gcode.Rs274.Lexer import GcodeLexer, GcodeLexerError
from PythonicGcodeMachine.Gcode.Rs274.Parser import GcodeParser, GcodeParserError
####################################################################################################
......
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