basic.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.basic
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for BASIC like languages (other than VB.net).
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, bygroups, default, words, include
  11. from pygments.token import Comment, Error, Keyword, Name, Number, \
  12. Punctuation, Operator, String, Text, Whitespace
  13. from pygments.lexers import _vbscript_builtins
  14. __all__ = ['BlitzBasicLexer', 'BlitzMaxLexer', 'MonkeyLexer', 'CbmBasicV2Lexer',
  15. 'QBasicLexer', 'VBScriptLexer', 'BBCBasicLexer']
  16. class BlitzMaxLexer(RegexLexer):
  17. """
  18. For `BlitzMax <http://blitzbasic.com>`_ source code.
  19. .. versionadded:: 1.4
  20. """
  21. name = 'BlitzMax'
  22. aliases = ['blitzmax', 'bmax']
  23. filenames = ['*.bmx']
  24. mimetypes = ['text/x-bmx']
  25. bmax_vopwords = r'\b(Shl|Shr|Sar|Mod)\b'
  26. bmax_sktypes = r'@{1,2}|[!#$%]'
  27. bmax_lktypes = r'\b(Int|Byte|Short|Float|Double|Long)\b'
  28. bmax_name = r'[a-z_]\w*'
  29. bmax_var = (r'(%s)(?:(?:([ \t]*)(%s)|([ \t]*:[ \t]*\b(?:Shl|Shr|Sar|Mod)\b)'
  30. r'|([ \t]*)(:)([ \t]*)(?:%s|(%s)))(?:([ \t]*)(Ptr))?)') % \
  31. (bmax_name, bmax_sktypes, bmax_lktypes, bmax_name)
  32. bmax_func = bmax_var + r'?((?:[ \t]|\.\.\n)*)([(])'
  33. flags = re.MULTILINE | re.IGNORECASE
  34. tokens = {
  35. 'root': [
  36. # Text
  37. (r'[ \t]+', Text),
  38. (r'\.\.\n', Text), # Line continuation
  39. # Comments
  40. (r"'.*?\n", Comment.Single),
  41. (r'([ \t]*)\bRem\n(\n|.)*?\s*\bEnd([ \t]*)Rem', Comment.Multiline),
  42. # Data types
  43. ('"', String.Double, 'string'),
  44. # Numbers
  45. (r'[0-9]+\.[0-9]*(?!\.)', Number.Float),
  46. (r'\.[0-9]*(?!\.)', Number.Float),
  47. (r'[0-9]+', Number.Integer),
  48. (r'\$[0-9a-f]+', Number.Hex),
  49. (r'\%[10]+', Number.Bin),
  50. # Other
  51. (r'(?:(?:(:)?([ \t]*)(:?%s|([+\-*/&|~]))|Or|And|Not|[=<>^]))' %
  52. (bmax_vopwords), Operator),
  53. (r'[(),.:\[\]]', Punctuation),
  54. (r'(?:#[\w \t]*)', Name.Label),
  55. (r'(?:\?[\w \t]*)', Comment.Preproc),
  56. # Identifiers
  57. (r'\b(New)\b([ \t]?)([(]?)(%s)' % (bmax_name),
  58. bygroups(Keyword.Reserved, Text, Punctuation, Name.Class)),
  59. (r'\b(Import|Framework|Module)([ \t]+)(%s\.%s)' %
  60. (bmax_name, bmax_name),
  61. bygroups(Keyword.Reserved, Text, Keyword.Namespace)),
  62. (bmax_func, bygroups(Name.Function, Text, Keyword.Type,
  63. Operator, Text, Punctuation, Text,
  64. Keyword.Type, Name.Class, Text,
  65. Keyword.Type, Text, Punctuation)),
  66. (bmax_var, bygroups(Name.Variable, Text, Keyword.Type, Operator,
  67. Text, Punctuation, Text, Keyword.Type,
  68. Name.Class, Text, Keyword.Type)),
  69. (r'\b(Type|Extends)([ \t]+)(%s)' % (bmax_name),
  70. bygroups(Keyword.Reserved, Text, Name.Class)),
  71. # Keywords
  72. (r'\b(Ptr)\b', Keyword.Type),
  73. (r'\b(Pi|True|False|Null|Self|Super)\b', Keyword.Constant),
  74. (r'\b(Local|Global|Const|Field)\b', Keyword.Declaration),
  75. (words((
  76. 'TNullMethodException', 'TNullFunctionException',
  77. 'TNullObjectException', 'TArrayBoundsException',
  78. 'TRuntimeException'), prefix=r'\b', suffix=r'\b'), Name.Exception),
  79. (words((
  80. 'Strict', 'SuperStrict', 'Module', 'ModuleInfo',
  81. 'End', 'Return', 'Continue', 'Exit', 'Public', 'Private',
  82. 'Var', 'VarPtr', 'Chr', 'Len', 'Asc', 'SizeOf', 'Sgn', 'Abs', 'Min', 'Max',
  83. 'New', 'Release', 'Delete', 'Incbin', 'IncbinPtr', 'IncbinLen',
  84. 'Framework', 'Include', 'Import', 'Extern', 'EndExtern',
  85. 'Function', 'EndFunction', 'Type', 'EndType', 'Extends', 'Method', 'EndMethod',
  86. 'Abstract', 'Final', 'If', 'Then', 'Else', 'ElseIf', 'EndIf',
  87. 'For', 'To', 'Next', 'Step', 'EachIn', 'While', 'Wend', 'EndWhile',
  88. 'Repeat', 'Until', 'Forever', 'Select', 'Case', 'Default', 'EndSelect',
  89. 'Try', 'Catch', 'EndTry', 'Throw', 'Assert', 'Goto', 'DefData', 'ReadData',
  90. 'RestoreData'), prefix=r'\b', suffix=r'\b'),
  91. Keyword.Reserved),
  92. # Final resolve (for variable names and such)
  93. (r'(%s)' % (bmax_name), Name.Variable),
  94. ],
  95. 'string': [
  96. (r'""', String.Double),
  97. (r'"C?', String.Double, '#pop'),
  98. (r'[^"]+', String.Double),
  99. ],
  100. }
  101. class BlitzBasicLexer(RegexLexer):
  102. """
  103. For `BlitzBasic <http://blitzbasic.com>`_ source code.
  104. .. versionadded:: 2.0
  105. """
  106. name = 'BlitzBasic'
  107. aliases = ['blitzbasic', 'b3d', 'bplus']
  108. filenames = ['*.bb', '*.decls']
  109. mimetypes = ['text/x-bb']
  110. bb_sktypes = r'@{1,2}|[#$%]'
  111. bb_name = r'[a-z]\w*'
  112. bb_var = (r'(%s)(?:([ \t]*)(%s)|([ \t]*)([.])([ \t]*)(?:(%s)))?') % \
  113. (bb_name, bb_sktypes, bb_name)
  114. flags = re.MULTILINE | re.IGNORECASE
  115. tokens = {
  116. 'root': [
  117. # Text
  118. (r'[ \t]+', Text),
  119. # Comments
  120. (r";.*?\n", Comment.Single),
  121. # Data types
  122. ('"', String.Double, 'string'),
  123. # Numbers
  124. (r'[0-9]+\.[0-9]*(?!\.)', Number.Float),
  125. (r'\.[0-9]+(?!\.)', Number.Float),
  126. (r'[0-9]+', Number.Integer),
  127. (r'\$[0-9a-f]+', Number.Hex),
  128. (r'\%[10]+', Number.Bin),
  129. # Other
  130. (words(('Shl', 'Shr', 'Sar', 'Mod', 'Or', 'And', 'Not',
  131. 'Abs', 'Sgn', 'Handle', 'Int', 'Float', 'Str',
  132. 'First', 'Last', 'Before', 'After'),
  133. prefix=r'\b', suffix=r'\b'),
  134. Operator),
  135. (r'([+\-*/~=<>^])', Operator),
  136. (r'[(),:\[\]\\]', Punctuation),
  137. (r'\.([ \t]*)(%s)' % bb_name, Name.Label),
  138. # Identifiers
  139. (r'\b(New)\b([ \t]+)(%s)' % (bb_name),
  140. bygroups(Keyword.Reserved, Text, Name.Class)),
  141. (r'\b(Gosub|Goto)\b([ \t]+)(%s)' % (bb_name),
  142. bygroups(Keyword.Reserved, Text, Name.Label)),
  143. (r'\b(Object)\b([ \t]*)([.])([ \t]*)(%s)\b' % (bb_name),
  144. bygroups(Operator, Text, Punctuation, Text, Name.Class)),
  145. (r'\b%s\b([ \t]*)(\()' % bb_var,
  146. bygroups(Name.Function, Text, Keyword.Type, Text, Punctuation,
  147. Text, Name.Class, Text, Punctuation)),
  148. (r'\b(Function)\b([ \t]+)%s' % bb_var,
  149. bygroups(Keyword.Reserved, Text, Name.Function, Text, Keyword.Type,
  150. Text, Punctuation, Text, Name.Class)),
  151. (r'\b(Type)([ \t]+)(%s)' % (bb_name),
  152. bygroups(Keyword.Reserved, Text, Name.Class)),
  153. # Keywords
  154. (r'\b(Pi|True|False|Null)\b', Keyword.Constant),
  155. (r'\b(Local|Global|Const|Field|Dim)\b', Keyword.Declaration),
  156. (words((
  157. 'End', 'Return', 'Exit', 'Chr', 'Len', 'Asc', 'New', 'Delete', 'Insert',
  158. 'Include', 'Function', 'Type', 'If', 'Then', 'Else', 'ElseIf', 'EndIf',
  159. 'For', 'To', 'Next', 'Step', 'Each', 'While', 'Wend',
  160. 'Repeat', 'Until', 'Forever', 'Select', 'Case', 'Default',
  161. 'Goto', 'Gosub', 'Data', 'Read', 'Restore'), prefix=r'\b', suffix=r'\b'),
  162. Keyword.Reserved),
  163. # Final resolve (for variable names and such)
  164. # (r'(%s)' % (bb_name), Name.Variable),
  165. (bb_var, bygroups(Name.Variable, Text, Keyword.Type,
  166. Text, Punctuation, Text, Name.Class)),
  167. ],
  168. 'string': [
  169. (r'""', String.Double),
  170. (r'"C?', String.Double, '#pop'),
  171. (r'[^"]+', String.Double),
  172. ],
  173. }
  174. class MonkeyLexer(RegexLexer):
  175. """
  176. For
  177. `Monkey <https://en.wikipedia.org/wiki/Monkey_(programming_language)>`_
  178. source code.
  179. .. versionadded:: 1.6
  180. """
  181. name = 'Monkey'
  182. aliases = ['monkey']
  183. filenames = ['*.monkey']
  184. mimetypes = ['text/x-monkey']
  185. name_variable = r'[a-z_]\w*'
  186. name_function = r'[A-Z]\w*'
  187. name_constant = r'[A-Z_][A-Z0-9_]*'
  188. name_class = r'[A-Z]\w*'
  189. name_module = r'[a-z0-9_]*'
  190. keyword_type = r'(?:Int|Float|String|Bool|Object|Array|Void)'
  191. # ? == Bool // % == Int // # == Float // $ == String
  192. keyword_type_special = r'[?%#$]'
  193. flags = re.MULTILINE
  194. tokens = {
  195. 'root': [
  196. # Text
  197. (r'\s+', Text),
  198. # Comments
  199. (r"'.*", Comment),
  200. (r'(?i)^#rem\b', Comment.Multiline, 'comment'),
  201. # preprocessor directives
  202. (r'(?i)^(?:#If|#ElseIf|#Else|#EndIf|#End|#Print|#Error)\b', Comment.Preproc),
  203. # preprocessor variable (any line starting with '#' that is not a directive)
  204. (r'^#', Comment.Preproc, 'variables'),
  205. # String
  206. ('"', String.Double, 'string'),
  207. # Numbers
  208. (r'[0-9]+\.[0-9]*(?!\.)', Number.Float),
  209. (r'\.[0-9]+(?!\.)', Number.Float),
  210. (r'[0-9]+', Number.Integer),
  211. (r'\$[0-9a-fA-Z]+', Number.Hex),
  212. (r'\%[10]+', Number.Bin),
  213. # Native data types
  214. (r'\b%s\b' % keyword_type, Keyword.Type),
  215. # Exception handling
  216. (r'(?i)\b(?:Try|Catch|Throw)\b', Keyword.Reserved),
  217. (r'Throwable', Name.Exception),
  218. # Builtins
  219. (r'(?i)\b(?:Null|True|False)\b', Name.Builtin),
  220. (r'(?i)\b(?:Self|Super)\b', Name.Builtin.Pseudo),
  221. (r'\b(?:HOST|LANG|TARGET|CONFIG)\b', Name.Constant),
  222. # Keywords
  223. (r'(?i)^(Import)(\s+)(.*)(\n)',
  224. bygroups(Keyword.Namespace, Text, Name.Namespace, Text)),
  225. (r'(?i)^Strict\b.*\n', Keyword.Reserved),
  226. (r'(?i)(Const|Local|Global|Field)(\s+)',
  227. bygroups(Keyword.Declaration, Text), 'variables'),
  228. (r'(?i)(New|Class|Interface|Extends|Implements)(\s+)',
  229. bygroups(Keyword.Reserved, Text), 'classname'),
  230. (r'(?i)(Function|Method)(\s+)',
  231. bygroups(Keyword.Reserved, Text), 'funcname'),
  232. (r'(?i)(?:End|Return|Public|Private|Extern|Property|'
  233. r'Final|Abstract)\b', Keyword.Reserved),
  234. # Flow Control stuff
  235. (r'(?i)(?:If|Then|Else|ElseIf|EndIf|'
  236. r'Select|Case|Default|'
  237. r'While|Wend|'
  238. r'Repeat|Until|Forever|'
  239. r'For|To|Until|Step|EachIn|Next|'
  240. r'Exit|Continue)\s+', Keyword.Reserved),
  241. # not used yet
  242. (r'(?i)\b(?:Module|Inline)\b', Keyword.Reserved),
  243. # Array
  244. (r'[\[\]]', Punctuation),
  245. # Other
  246. (r'<=|>=|<>|\*=|/=|\+=|-=|&=|~=|\|=|[-&*/^+=<>|~]', Operator),
  247. (r'(?i)(?:Not|Mod|Shl|Shr|And|Or)', Operator.Word),
  248. (r'[(){}!#,.:]', Punctuation),
  249. # catch the rest
  250. (r'%s\b' % name_constant, Name.Constant),
  251. (r'%s\b' % name_function, Name.Function),
  252. (r'%s\b' % name_variable, Name.Variable),
  253. ],
  254. 'funcname': [
  255. (r'(?i)%s\b' % name_function, Name.Function),
  256. (r':', Punctuation, 'classname'),
  257. (r'\s+', Text),
  258. (r'\(', Punctuation, 'variables'),
  259. (r'\)', Punctuation, '#pop')
  260. ],
  261. 'classname': [
  262. (r'%s\.' % name_module, Name.Namespace),
  263. (r'%s\b' % keyword_type, Keyword.Type),
  264. (r'%s\b' % name_class, Name.Class),
  265. # array (of given size)
  266. (r'(\[)(\s*)(\d*)(\s*)(\])',
  267. bygroups(Punctuation, Text, Number.Integer, Text, Punctuation)),
  268. # generics
  269. (r'\s+(?!<)', Text, '#pop'),
  270. (r'<', Punctuation, '#push'),
  271. (r'>', Punctuation, '#pop'),
  272. (r'\n', Text, '#pop'),
  273. default('#pop')
  274. ],
  275. 'variables': [
  276. (r'%s\b' % name_constant, Name.Constant),
  277. (r'%s\b' % name_variable, Name.Variable),
  278. (r'%s' % keyword_type_special, Keyword.Type),
  279. (r'\s+', Text),
  280. (r':', Punctuation, 'classname'),
  281. (r',', Punctuation, '#push'),
  282. default('#pop')
  283. ],
  284. 'string': [
  285. (r'[^"~]+', String.Double),
  286. (r'~q|~n|~r|~t|~z|~~', String.Escape),
  287. (r'"', String.Double, '#pop'),
  288. ],
  289. 'comment': [
  290. (r'(?i)^#rem.*?', Comment.Multiline, "#push"),
  291. (r'(?i)^#end.*?', Comment.Multiline, "#pop"),
  292. (r'\n', Comment.Multiline),
  293. (r'.+', Comment.Multiline),
  294. ],
  295. }
  296. class CbmBasicV2Lexer(RegexLexer):
  297. """
  298. For CBM BASIC V2 sources.
  299. .. versionadded:: 1.6
  300. """
  301. name = 'CBM BASIC V2'
  302. aliases = ['cbmbas']
  303. filenames = ['*.bas']
  304. flags = re.IGNORECASE
  305. tokens = {
  306. 'root': [
  307. (r'rem.*\n', Comment.Single),
  308. (r'\s+', Text),
  309. (r'new|run|end|for|to|next|step|go(to|sub)?|on|return|stop|cont'
  310. r'|if|then|input#?|read|wait|load|save|verify|poke|sys|print#?'
  311. r'|list|clr|cmd|open|close|get#?', Keyword.Reserved),
  312. (r'data|restore|dim|let|def|fn', Keyword.Declaration),
  313. (r'tab|spc|sgn|int|abs|usr|fre|pos|sqr|rnd|log|exp|cos|sin|tan|atn'
  314. r'|peek|len|val|asc|(str|chr|left|right|mid)\$', Name.Builtin),
  315. (r'[-+*/^<>=]', Operator),
  316. (r'not|and|or', Operator.Word),
  317. (r'"[^"\n]*.', String),
  318. (r'\d+|[-+]?\d*\.\d*(e[-+]?\d+)?', Number.Float),
  319. (r'[(),:;]', Punctuation),
  320. (r'\w+[$%]?', Name),
  321. ]
  322. }
  323. def analyse_text(self, text):
  324. # if it starts with a line number, it shouldn't be a "modern" Basic
  325. # like VB.net
  326. if re.match(r'\d+', text):
  327. return 0.2
  328. class QBasicLexer(RegexLexer):
  329. """
  330. For
  331. `QBasic <http://en.wikipedia.org/wiki/QBasic>`_
  332. source code.
  333. .. versionadded:: 2.0
  334. """
  335. name = 'QBasic'
  336. aliases = ['qbasic', 'basic']
  337. filenames = ['*.BAS', '*.bas']
  338. mimetypes = ['text/basic']
  339. declarations = ('DATA', 'LET')
  340. functions = (
  341. 'ABS', 'ASC', 'ATN', 'CDBL', 'CHR$', 'CINT', 'CLNG',
  342. 'COMMAND$', 'COS', 'CSNG', 'CSRLIN', 'CVD', 'CVDMBF', 'CVI',
  343. 'CVL', 'CVS', 'CVSMBF', 'DATE$', 'ENVIRON$', 'EOF', 'ERDEV',
  344. 'ERDEV$', 'ERL', 'ERR', 'EXP', 'FILEATTR', 'FIX', 'FRE',
  345. 'FREEFILE', 'HEX$', 'INKEY$', 'INP', 'INPUT$', 'INSTR', 'INT',
  346. 'IOCTL$', 'LBOUND', 'LCASE$', 'LEFT$', 'LEN', 'LOC', 'LOF',
  347. 'LOG', 'LPOS', 'LTRIM$', 'MID$', 'MKD$', 'MKDMBF$', 'MKI$',
  348. 'MKL$', 'MKS$', 'MKSMBF$', 'OCT$', 'PEEK', 'PEN', 'PLAY',
  349. 'PMAP', 'POINT', 'POS', 'RIGHT$', 'RND', 'RTRIM$', 'SADD',
  350. 'SCREEN', 'SEEK', 'SETMEM', 'SGN', 'SIN', 'SPACE$', 'SPC',
  351. 'SQR', 'STICK', 'STR$', 'STRIG', 'STRING$', 'TAB', 'TAN',
  352. 'TIME$', 'TIMER', 'UBOUND', 'UCASE$', 'VAL', 'VARPTR',
  353. 'VARPTR$', 'VARSEG'
  354. )
  355. metacommands = ('$DYNAMIC', '$INCLUDE', '$STATIC')
  356. operators = ('AND', 'EQV', 'IMP', 'NOT', 'OR', 'XOR')
  357. statements = (
  358. 'BEEP', 'BLOAD', 'BSAVE', 'CALL', 'CALL ABSOLUTE',
  359. 'CALL INTERRUPT', 'CALLS', 'CHAIN', 'CHDIR', 'CIRCLE', 'CLEAR',
  360. 'CLOSE', 'CLS', 'COLOR', 'COM', 'COMMON', 'CONST', 'DATA',
  361. 'DATE$', 'DECLARE', 'DEF FN', 'DEF SEG', 'DEFDBL', 'DEFINT',
  362. 'DEFLNG', 'DEFSNG', 'DEFSTR', 'DEF', 'DIM', 'DO', 'LOOP',
  363. 'DRAW', 'END', 'ENVIRON', 'ERASE', 'ERROR', 'EXIT', 'FIELD',
  364. 'FILES', 'FOR', 'NEXT', 'FUNCTION', 'GET', 'GOSUB', 'GOTO',
  365. 'IF', 'THEN', 'INPUT', 'INPUT #', 'IOCTL', 'KEY', 'KEY',
  366. 'KILL', 'LET', 'LINE', 'LINE INPUT', 'LINE INPUT #', 'LOCATE',
  367. 'LOCK', 'UNLOCK', 'LPRINT', 'LSET', 'MID$', 'MKDIR', 'NAME',
  368. 'ON COM', 'ON ERROR', 'ON KEY', 'ON PEN', 'ON PLAY',
  369. 'ON STRIG', 'ON TIMER', 'ON UEVENT', 'ON', 'OPEN', 'OPEN COM',
  370. 'OPTION BASE', 'OUT', 'PAINT', 'PALETTE', 'PCOPY', 'PEN',
  371. 'PLAY', 'POKE', 'PRESET', 'PRINT', 'PRINT #', 'PRINT USING',
  372. 'PSET', 'PUT', 'PUT', 'RANDOMIZE', 'READ', 'REDIM', 'REM',
  373. 'RESET', 'RESTORE', 'RESUME', 'RETURN', 'RMDIR', 'RSET', 'RUN',
  374. 'SCREEN', 'SEEK', 'SELECT CASE', 'SHARED', 'SHELL', 'SLEEP',
  375. 'SOUND', 'STATIC', 'STOP', 'STRIG', 'SUB', 'SWAP', 'SYSTEM',
  376. 'TIME$', 'TIMER', 'TROFF', 'TRON', 'TYPE', 'UEVENT', 'UNLOCK',
  377. 'VIEW', 'WAIT', 'WHILE', 'WEND', 'WIDTH', 'WINDOW', 'WRITE'
  378. )
  379. keywords = (
  380. 'ACCESS', 'ALIAS', 'ANY', 'APPEND', 'AS', 'BASE', 'BINARY',
  381. 'BYVAL', 'CASE', 'CDECL', 'DOUBLE', 'ELSE', 'ELSEIF', 'ENDIF',
  382. 'INTEGER', 'IS', 'LIST', 'LOCAL', 'LONG', 'LOOP', 'MOD',
  383. 'NEXT', 'OFF', 'ON', 'OUTPUT', 'RANDOM', 'SIGNAL', 'SINGLE',
  384. 'STEP', 'STRING', 'THEN', 'TO', 'UNTIL', 'USING', 'WEND'
  385. )
  386. tokens = {
  387. 'root': [
  388. (r'\n+', Text),
  389. (r'\s+', Text.Whitespace),
  390. (r'^(\s*)(\d*)(\s*)(REM .*)$',
  391. bygroups(Text.Whitespace, Name.Label, Text.Whitespace,
  392. Comment.Single)),
  393. (r'^(\s*)(\d+)(\s*)',
  394. bygroups(Text.Whitespace, Name.Label, Text.Whitespace)),
  395. (r'(?=[\s]*)(\w+)(?=[\s]*=)', Name.Variable.Global),
  396. (r'(?=[^"]*)\'.*$', Comment.Single),
  397. (r'"[^\n"]*"', String.Double),
  398. (r'(END)(\s+)(FUNCTION|IF|SELECT|SUB)',
  399. bygroups(Keyword.Reserved, Text.Whitespace, Keyword.Reserved)),
  400. (r'(DECLARE)(\s+)([A-Z]+)(\s+)(\S+)',
  401. bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable,
  402. Text.Whitespace, Name)),
  403. (r'(DIM)(\s+)(SHARED)(\s+)([^\s(]+)',
  404. bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable,
  405. Text.Whitespace, Name.Variable.Global)),
  406. (r'(DIM)(\s+)([^\s(]+)',
  407. bygroups(Keyword.Declaration, Text.Whitespace, Name.Variable.Global)),
  408. (r'^(\s*)([a-zA-Z_]+)(\s*)(\=)',
  409. bygroups(Text.Whitespace, Name.Variable.Global, Text.Whitespace,
  410. Operator)),
  411. (r'(GOTO|GOSUB)(\s+)(\w+\:?)',
  412. bygroups(Keyword.Reserved, Text.Whitespace, Name.Label)),
  413. (r'(SUB)(\s+)(\w+\:?)',
  414. bygroups(Keyword.Reserved, Text.Whitespace, Name.Label)),
  415. include('declarations'),
  416. include('functions'),
  417. include('metacommands'),
  418. include('operators'),
  419. include('statements'),
  420. include('keywords'),
  421. (r'[a-zA-Z_]\w*[$@#&!]', Name.Variable.Global),
  422. (r'[a-zA-Z_]\w*\:', Name.Label),
  423. (r'\-?\d*\.\d+[@|#]?', Number.Float),
  424. (r'\-?\d+[@|#]', Number.Float),
  425. (r'\-?\d+#?', Number.Integer.Long),
  426. (r'\-?\d+#?', Number.Integer),
  427. (r'!=|==|:=|\.=|<<|>>|[-~+/\\*%=<>&^|?:!.]', Operator),
  428. (r'[\[\]{}(),;]', Punctuation),
  429. (r'[\w]+', Name.Variable.Global),
  430. ],
  431. # can't use regular \b because of X$()
  432. # XXX: use words() here
  433. 'declarations': [
  434. (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, declarations)),
  435. Keyword.Declaration),
  436. ],
  437. 'functions': [
  438. (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, functions)),
  439. Keyword.Reserved),
  440. ],
  441. 'metacommands': [
  442. (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, metacommands)),
  443. Keyword.Constant),
  444. ],
  445. 'operators': [
  446. (r'\b(%s)(?=\(|\b)' % '|'.join(map(re.escape, operators)), Operator.Word),
  447. ],
  448. 'statements': [
  449. (r'\b(%s)\b' % '|'.join(map(re.escape, statements)),
  450. Keyword.Reserved),
  451. ],
  452. 'keywords': [
  453. (r'\b(%s)\b' % '|'.join(keywords), Keyword),
  454. ],
  455. }
  456. def analyse_text(text):
  457. if '$DYNAMIC' in text or '$STATIC' in text:
  458. return 0.9
  459. class VBScriptLexer(RegexLexer):
  460. """
  461. VBScript is scripting language that is modeled on Visual Basic.
  462. .. versionadded:: 2.4
  463. """
  464. name = 'VBScript'
  465. aliases = ['vbscript']
  466. filenames = ['*.vbs', '*.VBS']
  467. flags = re.IGNORECASE
  468. tokens = {
  469. 'root': [
  470. (r"'[^\n]*", Comment.Single),
  471. (r'\s+', Whitespace),
  472. ('"', String.Double, 'string'),
  473. ('&h[0-9a-f]+', Number.Hex),
  474. # Float variant 1, for example: 1., 1.e2, 1.2e3
  475. (r'[0-9]+\.[0-9]*(e[+-]?[0-9]+)?', Number.Float),
  476. (r'\.[0-9]+(e[+-]?[0-9]+)?', Number.Float), # Float variant 2, for example: .1, .1e2
  477. (r'[0-9]+e[+-]?[0-9]+', Number.Float), # Float variant 3, for example: 123e45
  478. (r'\d+', Number.Integer),
  479. ('#.+#', String), # date or time value
  480. (r'(dim)(\s+)([a-z_][a-z0-9_]*)',
  481. bygroups(Keyword.Declaration, Whitespace, Name.Variable), 'dim_more'),
  482. (r'(function|sub)(\s+)([a-z_][a-z0-9_]*)',
  483. bygroups(Keyword.Declaration, Whitespace, Name.Function)),
  484. (r'(class)(\s+)([a-z_][a-z0-9_]*)', bygroups(Keyword.Declaration, Whitespace, Name.Class)),
  485. (r'(const)(\s+)([a-z_][a-z0-9_]*)', bygroups(Keyword.Declaration, Whitespace, Name.Constant)),
  486. (r'(end)(\s+)(class|function|if|property|sub|with)', bygroups(Keyword, Whitespace, Keyword)),
  487. (r'(on)(\s+)(error)(\s+)(goto)(\s+)(0)',
  488. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword, Whitespace, Number.Integer)),
  489. (r'(on)(\s+)(error)(\s+)(resume)(\s+)(next)',
  490. bygroups(Keyword, Whitespace, Keyword, Whitespace, Keyword, Whitespace, Keyword)),
  491. (r'(option)(\s+)(explicit)', bygroups(Keyword, Whitespace, Keyword)),
  492. (r'(property)(\s+)(get|let|set)(\s+)([a-z_][a-z0-9_]*)',
  493. bygroups(Keyword.Declaration, Whitespace, Keyword.Declaration, Whitespace, Name.Property)),
  494. (r'rem\s.*[^\n]*', Comment.Single),
  495. (words(_vbscript_builtins.KEYWORDS, suffix=r'\b'), Keyword),
  496. (words(_vbscript_builtins.OPERATORS), Operator),
  497. (words(_vbscript_builtins.OPERATOR_WORDS, suffix=r'\b'), Operator.Word),
  498. (words(_vbscript_builtins.BUILTIN_CONSTANTS, suffix=r'\b'), Name.Constant),
  499. (words(_vbscript_builtins.BUILTIN_FUNCTIONS, suffix=r'\b'), Name.Builtin),
  500. (words(_vbscript_builtins.BUILTIN_VARIABLES, suffix=r'\b'), Name.Builtin),
  501. (r'[a-z_][a-z0-9_]*', Name),
  502. (r'\b_\n', Operator),
  503. (words(r'(),.:'), Punctuation),
  504. (r'.+(\n)?', Error)
  505. ],
  506. 'dim_more': [
  507. (r'(\s*)(,)(\s*)([a-z_][a-z0-9]*)', bygroups(Whitespace, Punctuation, Whitespace, Name.Variable)),
  508. default('#pop'),
  509. ],
  510. 'string': [
  511. (r'[^"\n]+', String.Double),
  512. (r'\"\"', String.Double),
  513. (r'"', String.Double, '#pop'),
  514. (r'\n', Error, '#pop'), # Unterminated string
  515. ],
  516. }
  517. class BBCBasicLexer(RegexLexer):
  518. """
  519. BBC Basic was supplied on the BBC Micro, and later Acorn RISC OS.
  520. It is also used by BBC Basic For Windows.
  521. .. versionadded:: 2.4
  522. """
  523. base_keywords = ['OTHERWISE', 'AND', 'DIV', 'EOR', 'MOD', 'OR', 'ERROR',
  524. 'LINE', 'OFF', 'STEP', 'SPC', 'TAB', 'ELSE', 'THEN',
  525. 'OPENIN', 'PTR', 'PAGE', 'TIME', 'LOMEM', 'HIMEM', 'ABS',
  526. 'ACS', 'ADVAL', 'ASC', 'ASN', 'ATN', 'BGET', 'COS', 'COUNT',
  527. 'DEG', 'ERL', 'ERR', 'EVAL', 'EXP', 'EXT', 'FALSE', 'FN',
  528. 'GET', 'INKEY', 'INSTR', 'INT', 'LEN', 'LN', 'LOG', 'NOT',
  529. 'OPENUP', 'OPENOUT', 'PI', 'POINT', 'POS', 'RAD', 'RND',
  530. 'SGN', 'SIN', 'SQR', 'TAN', 'TO', 'TRUE', 'USR', 'VAL',
  531. 'VPOS', 'CHR$', 'GET$', 'INKEY$', 'LEFT$', 'MID$',
  532. 'RIGHT$', 'STR$', 'STRING$', 'EOF', 'PTR', 'PAGE', 'TIME',
  533. 'LOMEM', 'HIMEM', 'SOUND', 'BPUT', 'CALL', 'CHAIN', 'CLEAR',
  534. 'CLOSE', 'CLG', 'CLS', 'DATA', 'DEF', 'DIM', 'DRAW', 'END',
  535. 'ENDPROC', 'ENVELOPE', 'FOR', 'GOSUB', 'GOTO', 'GCOL', 'IF',
  536. 'INPUT', 'LET', 'LOCAL', 'MODE', 'MOVE', 'NEXT', 'ON',
  537. 'VDU', 'PLOT', 'PRINT', 'PROC', 'READ', 'REM', 'REPEAT',
  538. 'REPORT', 'RESTORE', 'RETURN', 'RUN', 'STOP', 'COLOUR',
  539. 'TRACE', 'UNTIL', 'WIDTH', 'OSCLI']
  540. basic5_keywords = ['WHEN', 'OF', 'ENDCASE', 'ENDIF', 'ENDWHILE', 'CASE',
  541. 'CIRCLE', 'FILL', 'ORIGIN', 'POINT', 'RECTANGLE', 'SWAP',
  542. 'WHILE', 'WAIT', 'MOUSE', 'QUIT', 'SYS', 'INSTALL',
  543. 'LIBRARY', 'TINT', 'ELLIPSE', 'BEATS', 'TEMPO', 'VOICES',
  544. 'VOICE', 'STEREO', 'OVERLAY', 'APPEND', 'AUTO', 'CRUNCH',
  545. 'DELETE', 'EDIT', 'HELP', 'LIST', 'LOAD', 'LVAR', 'NEW',
  546. 'OLD', 'RENUMBER', 'SAVE', 'TEXTLOAD', 'TEXTSAVE',
  547. 'TWIN', 'TWINO', 'INSTALL', 'SUM', 'BEAT']
  548. name = 'BBC Basic'
  549. aliases = ['bbcbasic']
  550. filenames = ['*.bbc']
  551. tokens = {
  552. 'root': [
  553. (r"[0-9]+", Name.Label),
  554. (r"(\*)([^\n]*)",
  555. bygroups(Keyword.Pseudo, Comment.Special)),
  556. (r"", Whitespace, 'code'),
  557. ],
  558. 'code': [
  559. (r"(REM)([^\n]*)",
  560. bygroups(Keyword.Declaration, Comment.Single)),
  561. (r'\n', Whitespace, 'root'),
  562. (r'\s+', Whitespace),
  563. (r':', Comment.Preproc),
  564. # Some special cases to make functions come out nicer
  565. (r'(DEF)(\s*)(FN|PROC)([A-Za-z_@][\w@]*)',
  566. bygroups(Keyword.Declaration, Whitespace, Keyword.Declaration, Name.Function)),
  567. (r'(FN|PROC)([A-Za-z_@][\w@]*)',
  568. bygroups(Keyword, Name.Function)),
  569. (r'(GOTO|GOSUB|THEN|RESTORE)(\s*)(\d+)',
  570. bygroups(Keyword, Whitespace, Name.Label)),
  571. (r'(TRUE|FALSE)', Keyword.Constant),
  572. (r'(PAGE|LOMEM|HIMEM|TIME|WIDTH|ERL|ERR|REPORT\$|POS|VPOS|VOICES)', Keyword.Pseudo),
  573. (words(base_keywords), Keyword),
  574. (words(basic5_keywords), Keyword),
  575. ('"', String.Double, 'string'),
  576. ('%[01]{1,32}', Number.Bin),
  577. ('&[0-9a-f]{1,8}', Number.Hex),
  578. (r'[+-]?[0-9]+\.[0-9]*(E[+-]?[0-9]+)?', Number.Float),
  579. (r'[+-]?\.[0-9]+(E[+-]?[0-9]+)?', Number.Float),
  580. (r'[+-]?[0-9]+E[+-]?[0-9]+', Number.Float),
  581. (r'[+-]?\d+', Number.Integer),
  582. (r'([A-Za-z_@][\w@]*[%$]?)', Name.Variable),
  583. (r'([+\-]=|[$!|?+\-*/%^=><();]|>=|<=|<>|<<|>>|>>>|,)', Operator),
  584. ],
  585. 'string': [
  586. (r'[^"\n]+', String.Double),
  587. (r'"', String.Double, '#pop'),
  588. (r'\n', Error, 'root'), # Unterminated string
  589. ],
  590. }
  591. def analyse_text(text):
  592. if text.startswith('10REM >') or text.startswith('REM >'):
  593. return 0.9