summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2019-11-24 20:53:33 +0100
committerGeorg Brandl <georg@python.org>2019-11-25 12:11:30 +0100
commitf5271e82665d2f1421896573e6f8cfe677ff36a1 (patch)
tree6ced767847fb08b5f817c16152e240b2acd5fc3d
parent3269963e870845b6ac9ee7f71b087768c07244ff (diff)
downloadpygments-git-f5271e82665d2f1421896573e6f8cfe677ff36a1.tar.gz
MATLAB: improve detection and float boundaries.
- Detect `.m` files starting with a function definition as MATLAB, not ObjC (#1149). - Require word boundaries in regexes matching numbers and floats, to avoid mishighlighting `load 123file` as starting with a number.
-rw-r--r--pygments/lexers/matlab.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/pygments/lexers/matlab.py b/pygments/lexers/matlab.py
index 7aa2650e..65ac62d4 100644
--- a/pygments/lexers/matlab.py
+++ b/pygments/lexers/matlab.py
@@ -99,6 +99,12 @@ class MatlabLexer(RegexLexer):
# operators requiring escape for re:
(r'\.\*|\*|\+|\.\^|\.\\|\.\/|\/|\\', Operator),
+ # numbers (must come before punctuation to handle `.5`; cannot use
+ # `\b` due to e.g. `5. + .5`).
+ (r'(?<!\w)((\d+\.\d*)|(\d*\.\d+))([eEf][+-]?\d+)?(?!\w)', Number.Float),
+ (r'\b\d+[eEf][+-]?[0-9]+\b', Number.Float),
+ (r'\b\d+\b', Number.Integer),
+
# punctuation:
(r'\[|\]|\(|\)|\{|\}|:|@|\.|,', Punctuation),
(r'=|:|;', Punctuation),
@@ -107,10 +113,6 @@ class MatlabLexer(RegexLexer):
# (not great, but handles common cases...)
(r'(?<=[\w)\].])\'+', Operator),
- (r'(\d+\.\d*|\d*\.\d+)([eEf][+-]?[0-9]+)?', Number.Float),
- (r'\d+[eEf][+-]?[0-9]+', Number.Float),
- (r'\d+', Number.Integer),
-
(r'(?<![\w)\].])\'', String, 'string'),
(r'[a-zA-Z_]\w*', Name),
(r'.', Text),
@@ -134,9 +136,15 @@ class MatlabLexer(RegexLexer):
}
def analyse_text(text):
- if re.match(r'^\s*%', text, re.M): # comment
+ # function declaration.
+ if next(line for line in text.splitlines()
+ if not re.match(r'^\s*%', text)).strip().startswith('function'):
+ return 1.
+ # comment
+ elif re.match(r'^\s*%', text, re.M):
return 0.2
- elif re.match(r'^!\w+', text, re.M): # system cmd
+ # system cmd
+ elif re.match(r'^!\w+', text, re.M):
return 0.2