Pygments — newlines and white-space causing lexing errors

First, a big N.B. : I am not a Python programmer — I just know enough from reading the Pygments documentation and browsing the source of Pygments’ available lexers on Github to know how to write a Pygments lexer.


I have been working on a Jazz / SASM Saturn assembly syntax highlighter in Pygments. When I first began this endeavor, I thought the task would be extremely easy, but I soon ran into problems involving either quirks in how Pygments processes newlines and white-space, of which I don not understand the details and I have been pulling my hair out trying to get the lexer to work to no avail, hence my post here.

Jazz / SASM syntax has some white-space sensitive requirements, particularly that a label has to start in column one or two — I don’t know if this is part of the problem.

The first problem is that when a label starts in column two and is preceded by a blank line ( ie. just ‘n’ ) the label is marked as a lexing error or the whole line is marked as lexing error if a comment follows the label on the same line.

The second problem is quite strange and I have no idea what’s causing it : If a full-line comment ( which must start in column one with an asterisk ) is preceded by a blank line, then if there’s an instruction in the text that one would think would be commented out, instead it generates a lexing error for said instruction text.

For the third problem further explanation of Jazz / SASM syntax is needed : All conditional instructions must be followed by a ‘GOYES label’ or just a ‘RTNYES’ on the next line. The first sub-issue I’m encountering here is that if the conditional instruction has trailing spaces, which should be handled by the comment processing code, then Pygments marks said white-space as a lexing error. Additionally, the conditional instructions seem to be immune to whatever is causing the lexing errors associated with problem one ie. if the conditional instruction is preceded by a label on the same line, irrespective if it starts in column one or two and it’s preceded by a blank line, then Pygments seems to process the whole line without lexing errors. Lastly, the required ‘GOYES label’ or ‘RTNYES’ on the next line after the conditional is always marked as a lexing error by Pygments.

Now, I know all the above might not make a lot of sense because it is divorced from the code, so, WLOG, I’ve included the Pygments lexer code which just has two instructions : setting a register to zero and a conditional instruction that compares two registers.

from pygments.lexer import RegexLexer, bygroups
from pygments.token import *
import logging

class SasmJazzLexer(RegexLexer):
    name = 'SASM'
    aliases = ['SASM', 'Sasm', 'sasm', 'JAZZ', 'Jazz', 'jazz']
    filenames = ['*.A', '*.a']
    
    # Register fields
    fields = r'(P|WP|XS|X|S|M|B|A|W)'
    
    # Regex for label or symbol
    symbol = r'((?:[:=][(=:#0-9)][^, )]{,11})|(?:[:=]?[^(=:#0-9), ][^, ]{,11}))'
    
    
    tokens = {
        'root': [
            # Single line comment starting with '*' in column one
            (r'^*.*$', Comment.Single),
            
            # Whitespace or instructions
            (r'^( {2,})|(t)s*', Whitespace, 'possible_instructions'),
            
            # Label starting on column one or two and followed by a comment            
            (r'(^ ?)' + symbol + r'(s+*.*$)', bygroups(Whitespace, Name.Label, Comment.Single)),
            
            # Label starting in column one or two and followed by whitespace or instructions
            (r'(^ ?)' + symbol + r'(s+)', bygroups(Whitespace, Name.Label, Whitespace), 'possible_instructions'),
        ],
        'possible_instructions': [
            # Just whitespace or empty line -- emit token and bail
            (r'(s*$)|$', Whitespace, '#pop'),
            
            #
            # Register tests
            #
            
            (r'(?)([A-C])(=)([A-C])(' + r's+' + fields + r')', bygroups(Punctuation, Name.Builtin, Operator, Name.Builtin, Name.Builtin), ('possible_comment_cond', 'conditionals')),
            (r'(?)([CD])(=)([CD])(' + r's+' + fields + r')', bygroups(Punctuation, Name.Builtin, Operator, Name.Builtin, Name.Builtin), ('possible_comment_cond', 'conditionals')),
            (r'(?)(([A-D])(=)(3))(' + r's+' + fields + r')', bygroups(Punctuation, Error, Name.Builtin, Operator, Name.Builtin, Name.Builtin), ('possible_comment_cond', 'conditionals')),
            
            #
            # Arithmetic instructions
            #
            
            # r=0 fs
            # possibly followed by a comment
            (r'([A-D])(=)(0)(' + r's+' + fields + r')', bygroups(Name.Builtin, Operator, Name.Builtin, Name.Builtin), 'possible_comment'),
        ],
        'possible_comment': [
            #
            # Comment processing code
            #
            
            # Just whitespace or empty line -- emit token and bail
            (r'(s*$)|$', Whitespace, '#pop:2'),
            
            # Trailing comment
            (r's+S.*$', Comment.Single, '#pop:2')
        ],
        'possible_comment_cond': [
            #
            # Comment processing code for conditionals
            #
            
            # Just whitespace or empty line -- emit token and bail
            (r'(s*$)|$', Whitespace, '#pop'),
            
            # Trailing comment
            (r's+S.*$', Comment.Single, '#pop'),
        ],
        'conditionals': [
            #
            # GOYES and RTNYES processing code
            #
            
            (r'(^ {2,})(GOYES)(s+)' + symbol, bygroups(Whitespace, Keyword, Whitespace, Name.Label), 'possible_comment'),
            (r'(^ {2,})(RTNYES)', bygroups(Whitespace, Keyword), 'possible_comment'),

        ]
    }

In the above, if a line is not a full-line comment, then it either matches a label in column one or two which is possibly followed by an instruction ( in state ‘possible_instructions’ ) after which may follow an comment ( handled by the state ‘possible_comment’ which in this case there is no need to use an asterisk ) separated by the instruction by white-space.

For the conditional instructions, two states are pushed to the stack :

('possible_comment_cond', 'conditionals')

The ‘possible_comment_cond’ state is needed because every tuple in the ‘possible_comment’ state pops two stack levels which would pop the ‘conditionals’ state which is needed to process the ‘GOYES label’ or ‘RTNYES’ that must follow a conditional instruction.

Note that I have been generating a complete HTML document to test my lexer using

python3 -m pygments -f html -O full -o test.html -x -l pygments_sasm_lexer_5.py:SasmJazzLexer test.txt

Hopefully I’ve included enough information for this question to be understood — if I haven’t, please comment and I’ll add more details if necessary.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật