首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >lexer正则表达式pygments g-code

lexer正则表达式pygments g-code
EN

Stack Overflow用户
提问于 2016-07-08 02:14:35
回答 1查看 784关注 0票数 2

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

  • 突出显示行的注释(但不阻止comments);
  • highlight M和G命令(但不阻止其他命令X、Y、Z等。

gcodelexer.py来了

代码语言:javascript
复制
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),
        ]
    }

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

谢谢

更新:当前版本

代码语言:javascript
复制
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),
        ]
    }

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-11 06:26:34

对于任何需要gcode lexer的人来说,这里都有@Xander的帮助。如果你想对它的改进做出贡献,这里是官方的github repository

代码语言:javascript
复制
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),
        ]
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38252570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档