basic.py 27 KB

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