ThirdParty/Pygments/pygments/lexers/praat.py

changeset 4697
c2e9bf425554
child 5713
6762afd9f963
equal deleted inserted replaced
4696:bf4d19a7cade 4697:c2e9bf425554
1 # -*- coding: utf-8 -*-
2 """
3 pygments.lexers.praat
4 ~~~~~~~~~~~~~~~~~~~~~
5
6 Lexer for Praat
7
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 from pygments.lexer import RegexLexer, words, bygroups, include
13 from pygments.token import Name, Text, Comment, Keyword, String, Punctuation, Number, \
14 Operator
15
16 __all__ = ['PraatLexer']
17
18
19 class PraatLexer(RegexLexer):
20 """
21 For `Praat <http://www.praat.org>`_ scripts.
22
23 .. versionadded:: 2.1
24 """
25
26 name = 'Praat'
27 aliases = ['praat']
28 filenames = ['*.praat', '*.proc', '*.psc']
29
30 keywords = [
31 'if', 'then', 'else', 'elsif', 'elif', 'endif', 'fi', 'for', 'from', 'to',
32 'endfor', 'endproc', 'while', 'endwhile', 'repeat', 'until', 'select', 'plus',
33 'minus', 'demo', 'assert', 'stopwatch', 'nocheck', 'nowarn', 'noprogress',
34 'editor', 'endeditor', 'clearinfo',
35 ]
36
37 functions_string = [
38 'backslashTrigraphsToUnicode', 'chooseDirectory', 'chooseReadFile',
39 'chooseWriteFile', 'date', 'demoKey', 'do', 'environment', 'extractLine',
40 'extractWord', 'fixed', 'info', 'left', 'mid', 'percent', 'readFile', 'replace',
41 'replace_regex', 'right', 'selected', 'string', 'unicodeToBackslashTrigraphs',
42 ]
43
44 functions_numeric = [
45 'abs', 'appendFile', 'appendFileLine', 'appendInfo', 'appendInfoLine', 'arccos',
46 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'barkToHertz',
47 'beginPause', 'beginSendPraat', 'besselI', 'besselK', 'beta', 'beta2',
48 'binomialP', 'binomialQ', 'boolean', 'ceiling', 'chiSquareP', 'chiSquareQ',
49 'choice', 'comment', 'cos', 'cosh', 'createDirectory', 'deleteFile',
50 'demoClicked', 'demoClickedIn', 'demoCommandKeyPressed',
51 'demoExtraControlKeyPressed', 'demoInput', 'demoKeyPressed',
52 'demoOptionKeyPressed', 'demoShiftKeyPressed', 'demoShow', 'demoWaitForInput',
53 'demoWindowTitle', 'demoX', 'demoY', 'differenceLimensToPhon', 'do', 'editor',
54 'endPause', 'endSendPraat', 'endsWith', 'erb', 'erbToHertz', 'erf', 'erfc',
55 'exitScript', 'exp', 'extractNumber', 'fileReadable', 'fisherP', 'fisherQ',
56 'floor', 'gaussP', 'gaussQ', 'hertzToBark', 'hertzToErb', 'hertzToMel',
57 'hertzToSemitones', 'imax', 'imin', 'incompleteBeta', 'incompleteGammaP', 'index',
58 'index_regex', 'invBinomialP', 'invBinomialQ', 'invChiSquareQ', 'invFisherQ',
59 'invGaussQ', 'invSigmoid', 'invStudentQ', 'length', 'ln', 'lnBeta', 'lnGamma',
60 'log10', 'log2', 'max', 'melToHertz', 'min', 'minusObject', 'natural', 'number',
61 'numberOfColumns', 'numberOfRows', 'numberOfSelected', 'objectsAreIdentical',
62 'option', 'optionMenu', 'pauseScript', 'phonToDifferenceLimens', 'plusObject',
63 'positive', 'randomBinomial', 'randomGauss', 'randomInteger', 'randomPoisson',
64 'randomUniform', 'real', 'readFile', 'removeObject', 'rindex', 'rindex_regex',
65 'round', 'runScript', 'runSystem', 'runSystem_nocheck', 'selectObject',
66 'selected', 'semitonesToHertz', 'sentencetext', 'sigmoid', 'sin', 'sinc',
67 'sincpi', 'sinh', 'soundPressureToPhon', 'sqrt', 'startsWith', 'studentP',
68 'studentQ', 'tan', 'tanh', 'variableExists', 'word', 'writeFile', 'writeFileLine',
69 'writeInfo', 'writeInfoLine',
70 ]
71
72 functions_array = [
73 'linear', 'randomGauss', 'randomInteger', 'randomUniform', 'zero',
74 ]
75
76 objects = [
77 'Activation', 'AffineTransform', 'AmplitudeTier', 'Art', 'Artword',
78 'Autosegment', 'BarkFilter', 'BarkSpectrogram', 'CCA', 'Categories',
79 'Cepstrogram', 'Cepstrum', 'Cepstrumc', 'ChebyshevSeries', 'ClassificationTable',
80 'Cochleagram', 'Collection', 'ComplexSpectrogram', 'Configuration', 'Confusion',
81 'ContingencyTable', 'Corpus', 'Correlation', 'Covariance',
82 'CrossCorrelationTable', 'CrossCorrelationTables', 'DTW', 'DataModeler',
83 'Diagonalizer', 'Discriminant', 'Dissimilarity', 'Distance', 'Distributions',
84 'DurationTier', 'EEG', 'ERP', 'ERPTier', 'EditCostsTable', 'EditDistanceTable',
85 'Eigen', 'Excitation', 'Excitations', 'ExperimentMFC', 'FFNet', 'FeatureWeights',
86 'FileInMemory', 'FilesInMemory', 'Formant', 'FormantFilter', 'FormantGrid',
87 'FormantModeler', 'FormantPoint', 'FormantTier', 'GaussianMixture', 'HMM',
88 'HMM_Observation', 'HMM_ObservationSequence', 'HMM_State', 'HMM_StateSequence',
89 'Harmonicity', 'ISpline', 'Index', 'Intensity', 'IntensityTier', 'IntervalTier',
90 'KNN', 'KlattGrid', 'KlattTable', 'LFCC', 'LPC', 'Label', 'LegendreSeries',
91 'LinearRegression', 'LogisticRegression', 'LongSound', 'Ltas', 'MFCC', 'MSpline',
92 'ManPages', 'Manipulation', 'Matrix', 'MelFilter', 'MelSpectrogram',
93 'MixingMatrix', 'Movie', 'Network', 'OTGrammar', 'OTHistory', 'OTMulti', 'PCA',
94 'PairDistribution', 'ParamCurve', 'Pattern', 'Permutation', 'Photo', 'Pitch',
95 'PitchModeler', 'PitchTier', 'PointProcess', 'Polygon', 'Polynomial',
96 'PowerCepstrogram', 'PowerCepstrum', 'Procrustes', 'RealPoint', 'RealTier',
97 'ResultsMFC', 'Roots', 'SPINET', 'SSCP', 'SVD', 'Salience', 'ScalarProduct',
98 'Similarity', 'SimpleString', 'SortedSetOfString', 'Sound', 'Speaker',
99 'Spectrogram', 'Spectrum', 'SpectrumTier', 'SpeechSynthesizer', 'SpellingChecker',
100 'Strings', 'StringsIndex', 'Table', 'TableOfReal', 'TextGrid', 'TextInterval',
101 'TextPoint', 'TextTier', 'Tier', 'Transition', 'VocalTract', 'VocalTractTier',
102 'Weight', 'WordList',
103 ]
104
105 variables_numeric = [
106 'macintosh', 'windows', 'unix', 'praatVersion', 'pi', 'e', 'undefined',
107 ]
108
109 variables_string = [
110 'praatVersion', 'tab', 'shellDirectory', 'homeDirectory',
111 'preferencesDirectory', 'newline', 'temporaryDirectory',
112 'defaultDirectory',
113 ]
114
115 tokens = {
116 'root': [
117 (r'(\s+)(#.*?$)', bygroups(Text, Comment.Single)),
118 (r'^#.*?$', Comment.Single),
119 (r';[^\n]*', Comment.Single),
120 (r'\s+', Text),
121
122 (r'\bprocedure\b', Keyword, 'procedure_definition'),
123 (r'\bcall\b', Keyword, 'procedure_call'),
124 (r'@', Name.Function, 'procedure_call'),
125
126 include('function_call'),
127
128 (words(keywords, suffix=r'\b'), Keyword),
129
130 (r'(\bform\b)(\s+)([^\n]+)',
131 bygroups(Keyword, Text, String), 'old_form'),
132
133 (r'(print(?:line|tab)?|echo|exit|asserterror|pause|send(?:praat|socket)|'
134 r'include|execute|system(?:_nocheck)?)(\s+)',
135 bygroups(Keyword, Text), 'string_unquoted'),
136
137 (r'(goto|label)(\s+)(\w+)', bygroups(Keyword, Text, Name.Label)),
138
139 include('variable_name'),
140 include('number'),
141
142 (r'"', String, 'string'),
143
144 (words((objects), suffix=r'(?=\s+\S+\n)'), Name.Class, 'string_unquoted'),
145
146 (r'\b[A-Z]', Keyword, 'command'),
147 (r'(\.{3}|[)(,])', Punctuation),
148 ],
149 'command': [
150 (r'( ?[\w()-]+ ?)', Keyword),
151 (r"'(?=.*')", String.Interpol, 'string_interpolated'),
152 (r'\.{3}', Keyword, ('#pop', 'old_arguments')),
153 (r':', Keyword, ('#pop', 'comma_list')),
154 (r'[\s\n]', Text, '#pop'),
155 ],
156 'procedure_call': [
157 (r'\s+', Text),
158 (r'([\w.]+)(:|\s*\()',
159 bygroups(Name.Function, Text), '#pop'),
160 (r'([\w.]+)', Name.Function, ('#pop', 'old_arguments')),
161 ],
162 'procedure_definition': [
163 (r'\s', Text),
164 (r'([\w.]+)(\s*?[(:])',
165 bygroups(Name.Function, Text), '#pop'),
166 (r'([\w.]+)([^\n]*)',
167 bygroups(Name.Function, Text), '#pop'),
168 ],
169 'function_call': [
170 (words(functions_string, suffix=r'\$(?=\s*[:(])'), Name.Function, 'function'),
171 (words(functions_array, suffix=r'#(?=\s*[:(])'), Name.Function, 'function'),
172 (words(functions_numeric, suffix=r'(?=\s*[:(])'), Name.Function, 'function'),
173 ],
174 'function': [
175 (r'\s+', Text),
176 (r':', Punctuation, ('#pop', 'comma_list')),
177 (r'\s*\(', Punctuation, ('#pop', 'comma_list')),
178 ],
179 'comma_list': [
180 (r'(\s*\n\s*)(\.{3})', bygroups(Text, Punctuation)),
181
182 (r'(\s*[])\n])', Text, '#pop'),
183
184 (r'\s+', Text),
185 (r'"', String, 'string'),
186 (r'\b(if|then|else|fi|endif)\b', Keyword),
187
188 include('function_call'),
189 include('variable_name'),
190 include('operator'),
191 include('number'),
192
193 (r',', Punctuation),
194 ],
195 'old_arguments': [
196 (r'\n', Text, '#pop'),
197
198 include('variable_name'),
199 include('operator'),
200 include('number'),
201
202 (r'"', String, 'string'),
203 (r'[^\n]', Text),
204 ],
205 'number': [
206 (r'\b\d+(\.\d*)?([eE][-+]?\d+)?%?', Number),
207 ],
208 'object_attributes': [
209 (r'\.?(n(col|row)|[xy]min|[xy]max|[nd][xy])\b', Name.Builtin, '#pop'),
210 (r'(\.?(?:col|row)\$)(\[)',
211 bygroups(Name.Builtin, Text), 'variable_name'),
212 (r'(\$?)(\[)',
213 bygroups(Name.Builtin, Text), ('#pop', 'comma_list')),
214 ],
215 'variable_name': [
216 include('operator'),
217 include('number'),
218
219 (words(variables_string, suffix=r'\$'), Name.Variable.Global),
220 (words(variables_numeric, suffix=r'\b'), Name.Variable.Global),
221
222 (r'\bObject_\w+', Name.Builtin, 'object_attributes'),
223 (words(objects, prefix=r'\b', suffix=r'_\w+'),
224 Name.Builtin, 'object_attributes'),
225
226 (r"\b(Object_)(')",
227 bygroups(Name.Builtin, String.Interpol),
228 ('object_attributes', 'string_interpolated')),
229 (words(objects, prefix=r'\b', suffix=r"(_)(')"),
230 bygroups(Name.Builtin, Name.Builtin, String.Interpol),
231 ('object_attributes', 'string_interpolated')),
232
233 (r'\.?_?[a-z][a-zA-Z0-9_.]*(\$|#)?', Text),
234 (r'[\[\]]', Punctuation, 'comma_list'),
235 (r"'(?=.*')", String.Interpol, 'string_interpolated'),
236 ],
237 'operator': [
238 (r'([+\/*<>=!-]=?|[&*|][&*|]?|\^|<>)', Operator),
239 (r'\b(and|or|not|div|mod)\b', Operator.Word),
240 ],
241 'string_interpolated': [
242 (r'\.?[_a-z][a-zA-Z0-9_.]*[\$#]?(?:\[[a-zA-Z0-9,]+\])?(:[0-9]+)?',
243 String.Interpol),
244 (r"'", String.Interpol, '#pop'),
245 ],
246 'string_unquoted': [
247 (r'(\n\s*)(\.{3})', bygroups(Text, Punctuation)),
248
249 (r'\n', Text, '#pop'),
250 (r'\s', Text),
251 (r"'(?=.*')", String.Interpol, 'string_interpolated'),
252 (r"'", String),
253 (r"[^'\n]+", String),
254 ],
255 'string': [
256 (r'(\n\s*)(\.{3})', bygroups(Text, Punctuation)),
257
258 (r'"', String, '#pop'),
259 (r"'(?=.*')", String.Interpol, 'string_interpolated'),
260 (r"'", String),
261 (r'[^\'"\n]+', String),
262 ],
263 'old_form': [
264 (r'\s+', Text),
265
266 (r'(optionmenu|choice)([ \t]+\S+:[ \t]+)',
267 bygroups(Keyword, Text), 'number'),
268
269 (r'(option|button)([ \t]+)',
270 bygroups(Keyword, Text), 'number'),
271
272 (r'(option|button)([ \t]+)',
273 bygroups(Keyword, Text), 'string_unquoted'),
274
275 (r'(sentence|text)([ \t]+\S+)',
276 bygroups(Keyword, Text), 'string_unquoted'),
277
278 (r'(word)([ \t]+\S+[ \t]*)(\S+)?([ \t]+.*)?',
279 bygroups(Keyword, Text, String, Text)),
280
281 (r'(boolean)(\s+\S+\s*)(0|1|"?(?:yes|no)"?)',
282 bygroups(Keyword, Text, Name.Variable)),
283
284 # Ideally processing of the number would happend in the 'number'
285 # but that doesn't seem to work
286 (r'(real|natural|positive|integer)([ \t]+\S+[ \t]*)([+-]?)(\d+(?:\.\d*)?'
287 r'(?:[eE][-+]?\d+)?%?)',
288 bygroups(Keyword, Text, Operator, Number)),
289
290 (r'(comment)(\s+)',
291 bygroups(Keyword, Text), 'string_unquoted'),
292
293 (r'\bendform\b', Keyword, '#pop'),
294 ]
295 }

eric ide

mercurial