Skip to main content
 首页 » 编程设计

python之词法分析器正则表达式 pygments g 代码

2024年08月22日42over140

我正在尝试在 Pygments 上创建一个非常简单的词法分析器 http://pygments.org/docs/lexerdevelopment/ 这样我就可以在做一些有用的事情的同时获得一些 Python 的经验,然后继续创建一个更复杂的。词法分析器用于 g 代码,我能够:

  • 突出显示行注释(但不包括 block 注释);
  • 突出显示 M 和 G 命令(但不显示其他命令 X、Y、Z 等。

这是 gcodelexer.py

from pygments.lexer import RegexLexer 
from pygments.token import * 
 
__all__ = ['gcodeLexer'] 
 
class gcodeLexer(RegexLexer): 
    name = 'g-code' 
    aliases = ['gcode'] 
    filenames = ['*.gcode'] 
 
    tokens = { 
        'root': [ 
            (r' .*\n', Text), 
            (r';.*$', Comment), 
            (r'^[gmtGMT]\d{1,4}\s',Name.Builtin), # M or G commands 
            (r'[^gGmM][+-]?\d*[.]?\d+', Keyword), # other commands 
            # (r'\+.*\n', Generic.Inserted), 
            # (r'-.*\n', Generic.Deleted), 
            # (r'@.*\n', Generic.Subheading), 
            # (r'Index.*\n', Generic.Heading), 
            # (r'=.*\n', Generic.Heading), 
            (r'.*\n', Text), 
        ] 
    } 

基本上,“其他命令”每行只能找到两三个命令中的第一个,我不明白为什么……我还试图找到每个标记的描述(关键字、名称、运算符等,但是没有成功)。也许他们的名字应该是不言自明的?

谢谢

更新:当前版本

from pygments.lexer import RegexLexer 
from pygments.token import * 
 
__all__ = ['gcodeLexer'] 
 
class gcodeLexer(RegexLexer): 
    name = 'g-code' 
    aliases = ['gcode'] 
    filenames = ['*.gcode'] 
 
    tokens = { 
        'root': [ 
            (r'^;.*$', Comment), 
            (r'\s;.*', Comment.Multiline, 'blockcomment'), 
            (r'^[gmtGMT]\d{1,4}\s',Name.Builtin), # M or G commands 
            (r'[^gGmM][+-]?\d*[.]?\d+', Keyword), 
            (r'\s', Text.Whitespace), 
            (r'.*\n', Text), 
        ], 
        'blockcomment': [ 
            (r'.*;.*$', Comment.Multiline, '#pop'), 
            (r'^.*\n', Comment.Multiline), 
            (r'.', Comment.Multiline), 
        ] 
    } 

[IMG] http://i64.tinypic.com/2h4j7lw.png[/IMG]

M190 S50.000000 
M109 S250.000000 
;Sliced at: Sun 03-07-2016 17:55:50 
;Basic settings: Layer height: 0.3 Walls: 1.2 Fill: 20 
;Print time: 1 hour 9 minutes 
;Filament used: 2.584m 20.0g 
;Filament cost: 0.37 
;M190 S50 ;Uncomment to add your own bed temperature line 
;M109 S250 ;Uncomment to add your own temperature line 
G21        ;metric values 
G90        ;absolute positioning 
M82        ;set extruder to absolute mode 
G28 X0 Y0  ;move X/Y to min endstops 
G0 X100 Y100 
G28 Z0     ;move Z to min endstops 
G29 
G1 Z15.0 F100 ;move the platform down 15mm 
G92 E0                  ;zero the extruded length 
G1 F200 E3              ;extrude 3mm of feed stock 
G92 E0                  ;zero the extruded length again 
G1 F10800 
;Put printing message on LCD screen 
;?IF_EXT0?M109 T0 S?TEMP0? 
M117 Printing... 
 
;Layer count: 19 
;LAYER:0 
M107 
G0 F10800 X48.217 Y22.131 Z0.300 
;TYPE:SKIRT 
G1 F1800 X48.687 Y21.229 E0.01913 
G1 X48.936 Y20.744 E0.02939 
G1 X49.723 Y19.693 E0.05409 
G1 X50.013 Y19.303 E0.06323 
G1 X51.064 Y18.293 E0.09065 
G1 X51.455 Y17.957 E0.10034 

请您参考如下方法:

对于任何需要 gcode 词法分析器的人来说,这是在@Xander 的帮助下。如果您想为其改进做出贡献,请访问官方 github repository

from pygments.lexer import RegexLexer 
from pygments.token import * 
 
__all__ = ['gcodeLexer'] 
 
class gcodeLexer(RegexLexer): 
    name = 'g-code' 
    aliases = ['gcode'] 
    filenames = ['*.gcode'] 
 
    tokens = { 
        'root': [ 
            (r'^;.*$', Comment), 
            (r'\s;.*', Comment.Multiline, 'blockcomment'), 
            (r'^[gmtGMT]\d{1,4}\s',Name.Builtin), # M or G commands 
            (r'[^gGmM][+-]?\d*[.]?\d+', Keyword), 
            (r'\s', Text.Whitespace), 
            (r'.*\n', Text), 
        ], 
        'blockcomment': [ 
            (r'.*;.*$', Comment.Multiline, '#pop'), 
            (r'^.*\n', Comment.Multiline), 
            (r'.', Comment.Multiline), 
        ] 
    }