smalltalk.py 7.0 KB

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