Skip to content

Commit c3d93ad

Browse files
h1whelanclaude
andauthored
Fix ASN.1 lexer: recognize minus sign and fix range operator (#3060)
Two fixes for the ASN.1 lexer: 1. Add `-` to the operator pattern so negative values in constraints like `(-50..150)` are tokenized correctly instead of producing an error token for the minus sign. 2. Require at least one digit after the decimal point in float literals (`\d+\.\d+` instead of `\d+\.\d*`). This prevents `50..150` from being incorrectly tokenized as float `50.` + punctuation `.` + int `150`, instead of the correct int `50` + range `..` + int `150`. Fixes #3014. Co-authored-by: h1whelan <[email protected]> Co-authored-by: Claude Opus 4.6 <[email protected]>
1 parent 4f06bcf commit c3d93ad

3 files changed

Lines changed: 73 additions & 54 deletions

File tree

pygments/lexers/asn1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class Asn1Lexer(RegexLexer):
136136
(r'--.*$', Comment.Single),
137137
(r'/\*', Comment.Multiline, 'comment'),
138138
# Numbers:
139-
(r'\d+\.\d*([eE][-+]?\d+)?', Number.Float),
139+
(r'\d+\.\d+([eE][-+]?\d+)?', Number.Float),
140140
(r'\d+', Number.Integer),
141141
# Identifier:
142142
(r"&?[a-z][-a-zA-Z0-9]*[a-zA-Z0-9]\b", Name.Variable),
@@ -155,7 +155,7 @@ class Asn1Lexer(RegexLexer):
155155
# Type identifier:
156156
(r"&?[A-Z][-a-zA-Z0-9]*[a-zA-Z0-9]\b", Name.Type),
157157
# Operators:
158-
(r"(::=|\.\.\.|\.\.|\[\[|\]\]|\||\^)", Operator),
158+
(r"(::=|\.\.\.|\.\.|\[\[|\]\]|\||\^|-)", Operator),
159159
# Punctuation:
160160
(r"(\.|,|\{|\}|\(|\)|\[|\])", Punctuation),
161161
# String:

tests/examplefiles/asn1/x509.asn1.output

Lines changed: 52 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---input---
2+
Temperature ::= INTEGER (-50..150) -- Temperature in Celsius
3+
4+
---tokens---
5+
'Temperature' Name.Type
6+
' ' Text.Whitespace
7+
'::=' Operator
8+
' ' Text.Whitespace
9+
'INTEGER' Keyword.Type
10+
' ' Text.Whitespace
11+
'(' Punctuation
12+
'-' Operator
13+
'50' Literal.Number.Integer
14+
'..' Operator
15+
'150' Literal.Number.Integer
16+
')' Punctuation
17+
' ' Text.Whitespace
18+
'-- Temperature in Celsius' Comment.Single
19+
'\n' Text.Whitespace

0 commit comments

Comments
 (0)