ThirdParty/Pygments/pygments/lexers/smalltalk.py

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

eric ide

mercurial