basic.py 27 KB

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