smalltalk.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.smalltalk
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for Smalltalk and related languages.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, include, bygroups, default
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['SmalltalkLexer', 'NewspeakLexer']
  13. class SmalltalkLexer(RegexLexer):
  14. """
  15. For `Smalltalk <http://www.smalltalk.org/>`_ syntax.
  16. Contributed by Stefan Matthias Aust.
  17. Rewritten by Nils Winter.
  18. .. versionadded:: 0.10
  19. """
  20. name = 'Smalltalk'
  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 <http://newspeaklanguage.org/>` syntax.
  133. .. versionadded:: 1.1
  134. """
  135. name = 'Newspeak'
  136. filenames = ['*.ns2']
  137. aliases = ['newspeak', ]
  138. mimetypes = ['text/x-newspeak']
  139. tokens = {
  140. 'root': [
  141. (r'\b(Newsqueak2)\b', Keyword.Declaration),
  142. (r"'[^']*'", String),
  143. (r'\b(class)(\s+)(\w+)(\s*)',
  144. bygroups(Keyword.Declaration, Text, Name.Class, Text)),
  145. (r'\b(mixin|self|super|private|public|protected|nil|true|false)\b',
  146. Keyword),
  147. (r'(\w+\:)(\s*)([a-zA-Z_]\w+)',
  148. bygroups(Name.Function, Text, Name.Variable)),
  149. (r'(\w+)(\s*)(=)',
  150. bygroups(Name.Attribute, Text, Operator)),
  151. (r'<\w+>', Comment.Special),
  152. include('expressionstat'),
  153. include('whitespace')
  154. ],
  155. 'expressionstat': [
  156. (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
  157. (r'\d+', Number.Integer),
  158. (r':\w+', Name.Variable),
  159. (r'(\w+)(::)', bygroups(Name.Variable, Operator)),
  160. (r'\w+:', Name.Function),
  161. (r'\w+', Name.Variable),
  162. (r'\(|\)', Punctuation),
  163. (r'\[|\]', Punctuation),
  164. (r'\{|\}', Punctuation),
  165. (r'(\^|\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-|:)', Operator),
  166. (r'\.|;', Punctuation),
  167. include('whitespace'),
  168. include('literals'),
  169. ],
  170. 'literals': [
  171. (r'\$.', String),
  172. (r"'[^']*'", String),
  173. (r"#'[^']*'", String.Symbol),
  174. (r"#\w+:?", String.Symbol),
  175. (r"#(\+|\/|~|\*|<|>|=|@|%|\||&|\?|!|,|-)+", String.Symbol)
  176. ],
  177. 'whitespace': [
  178. (r'\s+', Text),
  179. (r'"[^"]*"', Comment)
  180. ],
  181. }