smalltalk.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. pygments.lexers.smalltalk
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Smalltalk and related languages.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, include, bygroups, default
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation
  11. __all__ = ['SmalltalkLexer', 'NewspeakLexer']
  12. class SmalltalkLexer(RegexLexer):
  13. """
  14. For Smalltalk syntax.
  15. Contributed by Stefan Matthias Aust.
  16. Rewritten by Nils Winter.
  17. .. versionadded:: 0.10
  18. """
  19. name = 'Smalltalk'
  20. url = 'http://www.smalltalk.org/'
  21. filenames = ['*.st']
  22. aliases = ['smalltalk', 'squeak', 'st']
  23. mimetypes = ['text/x-smalltalk']
  24. tokens = {
  25. 'root': [
  26. (r'(<)(\w+:)(.*?)(>)', bygroups(Text, Keyword, Text, Text)),
  27. include('squeak fileout'),
  28. include('whitespaces'),
  29. include('method definition'),
  30. (r'(\|)([\w\s]*)(\|)', bygroups(Operator, Name.Variable, Operator)),
  31. include('objects'),
  32. (r'\^|\:=|\_', Operator),
  33. # temporaries
  34. (r'[\]({}.;!]', Text),
  35. ],
  36. 'method definition': [
  37. # Not perfect can't allow whitespaces at the beginning and the
  38. # without breaking everything
  39. (r'([a-zA-Z]+\w*:)(\s*)(\w+)',
  40. bygroups(Name.Function, Text, Name.Variable)),
  41. (r'^(\b[a-zA-Z]+\w*\b)(\s*)$', bygroups(Name.Function, Text)),
  42. (r'^([-+*/\\~<>=|&!?,@%]+)(\s*)(\w+)(\s*)$',
  43. bygroups(Name.Function, Text, Name.Variable, Text)),
  44. ],
  45. 'blockvariables': [
  46. include('whitespaces'),
  47. (r'(:)(\s*)(\w+)',
  48. bygroups(Operator, Text, Name.Variable)),
  49. (r'\|', Operator, '#pop'),
  50. default('#pop'), # else pop
  51. ],
  52. 'literals': [
  53. (r"'(''|[^'])*'", String, 'afterobject'),
  54. (r'\$.', String.Char, 'afterobject'),
  55. (r'#\(', String.Symbol, 'parenth'),
  56. (r'\)', Text, 'afterobject'),
  57. (r'(\d+r)?-?\d+(\.\d+)?(e-?\d+)?', Number, 'afterobject'),
  58. ],
  59. '_parenth_helper': [
  60. include('whitespaces'),
  61. (r'(\d+r)?-?\d+(\.\d+)?(e-?\d+)?', Number),
  62. (r'[-+*/\\~<>=|&#!?,@%\w:]+', String.Symbol),
  63. # literals
  64. (r"'(''|[^'])*'", String),
  65. (r'\$.', String.Char),
  66. (r'#*\(', String.Symbol, 'inner_parenth'),
  67. ],
  68. 'parenth': [
  69. # This state is a bit tricky since
  70. # we can't just pop this state
  71. (r'\)', String.Symbol, ('root', 'afterobject')),
  72. include('_parenth_helper'),
  73. ],
  74. 'inner_parenth': [
  75. (r'\)', String.Symbol, '#pop'),
  76. include('_parenth_helper'),
  77. ],
  78. 'whitespaces': [
  79. # skip whitespace and comments
  80. (r'\s+', Text),
  81. (r'"(""|[^"])*"', Comment),
  82. ],
  83. 'objects': [
  84. (r'\[', Text, 'blockvariables'),
  85. (r'\]', Text, 'afterobject'),
  86. (r'\b(self|super|true|false|nil|thisContext)\b',
  87. Name.Builtin.Pseudo, 'afterobject'),
  88. (r'\b[A-Z]\w*(?!:)\b', Name.Class, 'afterobject'),
  89. (r'\b[a-z]\w*(?!:)\b', Name.Variable, 'afterobject'),
  90. (r'#("(""|[^"])*"|[-+*/\\~<>=|&!?,@%]+|[\w:]+)',
  91. String.Symbol, 'afterobject'),
  92. include('literals'),
  93. ],
  94. 'afterobject': [
  95. (r'! !$', Keyword, '#pop'), # squeak chunk delimiter
  96. include('whitespaces'),
  97. (r'\b(ifTrue:|ifFalse:|whileTrue:|whileFalse:|timesRepeat:)',
  98. Name.Builtin, '#pop'),
  99. (r'\b(new\b(?!:))', Name.Builtin),
  100. (r'\:=|\_', Operator, '#pop'),
  101. (r'\b[a-zA-Z]+\w*:', Name.Function, '#pop'),
  102. (r'\b[a-zA-Z]+\w*', Name.Function),
  103. (r'\w+:?|[-+*/\\~<>=|&!?,@%]+', Name.Function, '#pop'),
  104. (r'\.', Punctuation, '#pop'),
  105. (r';', Punctuation),
  106. (r'[\])}]', Text),
  107. (r'[\[({]', Text, '#pop'),
  108. ],
  109. 'squeak fileout': [
  110. # Squeak fileout format (optional)
  111. (r'^"(""|[^"])*"!', Keyword),
  112. (r"^'(''|[^'])*'!", Keyword),
  113. (r'^(!)(\w+)( commentStamp: )(.*?)( prior: .*?!\n)(.*?)(!)',
  114. bygroups(Keyword, Name.Class, Keyword, String, Keyword, Text, Keyword)),
  115. (r"^(!)(\w+(?: class)?)( methodsFor: )('(?:''|[^'])*')(.*?!)",
  116. bygroups(Keyword, Name.Class, Keyword, String, Keyword)),
  117. (r'^(\w+)( subclass: )(#\w+)'
  118. r'(\s+instanceVariableNames: )(.*?)'
  119. r'(\s+classVariableNames: )(.*?)'
  120. r'(\s+poolDictionaries: )(.*?)'
  121. r'(\s+category: )(.*?)(!)',
  122. bygroups(Name.Class, Keyword, String.Symbol, Keyword, String, Keyword,
  123. String, Keyword, String, Keyword, String, Keyword)),
  124. (r'^(\w+(?: class)?)(\s+instanceVariableNames: )(.*?)(!)',
  125. bygroups(Name.Class, Keyword, String, Keyword)),
  126. (r'(!\n)(\].*)(! !)$', bygroups(Keyword, Text, Keyword)),
  127. (r'! !$', Keyword),
  128. ],
  129. }
  130. class NewspeakLexer(RegexLexer):
  131. """
  132. For Newspeak syntax.
  133. .. versionadded:: 1.1
  134. """
  135. name = 'Newspeak'
  136. url = 'http://newspeaklanguage.org/'
  137. filenames = ['*.ns2']
  138. aliases = ['newspeak', ]
  139. mimetypes = ['text/x-newspeak']
  140. tokens = {
  141. 'root': [
  142. (r'\b(Newsqueak2)\b', Keyword.Declaration),
  143. (r"'[^']*'", String),
  144. (r'\b(class)(\s+)(\w+)(\s*)',
  145. bygroups(Keyword.Declaration, Text, Name.Class, Text)),
  146. (r'\b(mixin|self|super|private|public|protected|nil|true|false)\b',
  147. Keyword),
  148. (r'(\w+\:)(\s*)([a-zA-Z_]\w+)',
  149. bygroups(Name.Function, Text, Name.Variable)),
  150. (r'(\w+)(\s*)(=)',
  151. bygroups(Name.Attribute, Text, Operator)),
  152. (r'<\w+>', Comment.Special),
  153. include('expressionstat'),
  154. include('whitespace')
  155. ],
  156. 'expressionstat': [
  157. (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
  158. (r'\d+', Number.Integer),
  159. (r':\w+', Name.Variable),
  160. (r'(\w+)(::)', bygroups(Name.Variable, Operator)),
  161. (r'\w+:', Name.Function),
  162. (r'\w+', Name.Variable),
  163. (r'\(|\)', Punctuation),
  164. (r'\[|\]', Punctuation),
  165. (r'\{|\}', Punctuation),
  166. (r'(\^|\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-|:)', Operator),
  167. (r'\.|;', Punctuation),
  168. include('whitespace'),
  169. include('literals'),
  170. ],
  171. 'literals': [
  172. (r'\$.', String),
  173. (r"'[^']*'", String),
  174. (r"#'[^']*'", String.Symbol),
  175. (r"#\w+:?", String.Symbol),
  176. (r"#(\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-)+", String.Symbol)
  177. ],
  178. 'whitespace': [
  179. (r'\s+', Text),
  180. (r'"[^"]*"', Comment)
  181. ],
  182. }