basic.py 27 KB

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