summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2014-04-24 11:38:52 -0400
committerTim Hatch <tim@timhatch.com>2014-04-24 11:38:52 -0400
commit4330942e358d466ec6516e4114865ec6ca641c48 (patch)
tree522d4de78d70f6acf59f1118c1512fd5649b1aa6
parentd1e781ec268ad6d1b3af570412245af4e8608770 (diff)
downloadpygments-4330942e358d466ec6516e4114865ec6ca641c48.tar.gz
Let qbasic lexer match functions like 'RIGHT$', with test
-rw-r--r--pygments/lexers/qbasic.py28
-rw-r--r--tests/examplefiles/qbasic_example2
-rw-r--r--tests/test_qbasiclexer.py43
3 files changed, 59 insertions, 14 deletions
diff --git a/pygments/lexers/qbasic.py b/pygments/lexers/qbasic.py
index b84d36a9..80b80f9f 100644
--- a/pygments/lexers/qbasic.py
+++ b/pygments/lexers/qbasic.py
@@ -85,6 +85,8 @@ class QBasicLexer(RegexLexer):
tokens = {
'root': [
+ (r'\n+', Text),
+ (r'\s+', Text.Whitespace),
(r'^(\s*)(\d*)(\s*)(REM .*)$',
bygroups(Text.Whitespace, Name.Label, Text.Whitespace,
Comment.Single)),
@@ -93,7 +95,8 @@ class QBasicLexer(RegexLexer):
(r'(?=[\s]*)(\w+)(?=[\s]*=)', Name.Variable.Global),
(r'(?=[^"]*)\'.*$', Comment.Single),
(r'"[^\n\"]*"', String.Double),
- (r'END (FUNCTION|IF|SELECT|SUB)', Keyword.Reserved),
+ (r'(END)(\s+)(FUNCTION|IF|SELECT|SUB)',
+ bygroups(Keyword.Reserved, Text.Whitespace, Keyword.Reserved)),
(r'(DECLARE)(\s+)([A-Z]+)(\s+)(\S+)',
bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable,
Text.Whitespace, Name)),
@@ -109,6 +112,12 @@ class QBasicLexer(RegexLexer):
bygroups(Keyword.Reserved, Text.Whitespace, Name.Label)),
(r'(SUB)(\s+)(\w+\:?)',
bygroups(Keyword.Reserved, Text.Whitespace, Name.Label)),
+ include('declarations'),
+ include('functions'),
+ include('metacommands'),
+ include('operators'),
+ include('statements'),
+ include('keywords'),
(r'[a-zA-Z_]\w*[\$@#&!]', Name.Variable.Global),
(r'[a-zA-Z_]\w*\:', Name.Label),
(r'\-?\d*\.\d+[@|#]?', Number.Float),
@@ -116,31 +125,24 @@ class QBasicLexer(RegexLexer):
(r'\-?\d+#?', Number.Integer.Long),
(r'\-?\d+#?', Number.Integer),
(r'!=|==|:=|\.=|<<|>>|[-~+/\\*%=<>&^|?:!.]', Operator),
- include('declarations'),
- include('functions'),
- include('metacommands'),
- include('operators'),
- include('statements'),
- include('keywords'),
(r'[\[\]{}(),;]', Punctuation),
- (r'[\n]+', Text),
- (r'[\s]+', Text.Whitespace),
(r'[\w]+', Name.Variable.Global),
],
+ # can't use regular \b because of X$()
'declarations': [
- (r'\b(%s)\b' % '|'.join(map(re.escape, declarations)),
+ (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, declarations)),
Keyword.Declaration),
],
'functions': [
- (r'\b(%s)\b' % '|'.join(map(re.escape, functions)),
+ (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, functions)),
Keyword.Reserved),
],
'metacommands': [
- (r'\b(%s)\b' % '|'.join(map(re.escape, metacommands)),
+ (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, metacommands)),
Keyword.Constant),
],
'operators': [
- (r'\b(%s)\b' % '|'.join(map(re.escape, operators)), Operator.Word),
+ (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, operators)), Operator.Word),
],
'statements': [
(r'\b(%s)\b' % '|'.join(map(re.escape, statements)),
diff --git a/tests/examplefiles/qbasic_example b/tests/examplefiles/qbasic_example
index a5055175..27041af6 100644
--- a/tests/examplefiles/qbasic_example
+++ b/tests/examplefiles/qbasic_example
@@ -1,2 +1,2 @@
-10 print "hi"
+10 print RIGHT$("hi there", 5)
20 goto 10
diff --git a/tests/test_qbasiclexer.py b/tests/test_qbasiclexer.py
new file mode 100644
index 00000000..1b81b643
--- /dev/null
+++ b/tests/test_qbasiclexer.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+"""
+ Tests for QBasic
+ ~~~~~~~~~~~~~~~~
+
+ :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import glob
+import os
+import unittest
+
+from pygments.token import Token
+from pygments.lexers.qbasic import QBasicLexer
+
+class QBasicTest(unittest.TestCase):
+ def setUp(self):
+ self.lexer = QBasicLexer()
+ self.maxDiff = None
+
+ def testKeywordsWithDollar(self):
+ fragment = u'DIM x\nx = RIGHT$("abc", 1)\n'
+ expected = [
+ (Token.Keyword.Declaration, u'DIM'),
+ (Token.Text.Whitespace, u' '),
+ (Token.Name.Variable.Global, u'x'),
+ (Token.Text, u'\n'),
+ (Token.Name.Variable.Global, u'x'),
+ (Token.Text.Whitespace, u' '),
+ (Token.Operator, u'='),
+ (Token.Text.Whitespace, u' '),
+ (Token.Keyword.Reserved, u'RIGHT$'),
+ (Token.Punctuation, u'('),
+ (Token.Literal.String.Double, u'"abc"'),
+ (Token.Punctuation, u','),
+ (Token.Text.Whitespace, u' '),
+ (Token.Literal.Number.Integer.Long, u'1'),
+ (Token.Punctuation, u')'),
+ (Token.Text, u'\n'),
+ ]
+ self.assertEqual(expected, list(self.lexer.get_tokens(fragment)))
+