|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.praat |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for Praat |
|
7 |
|
8 :copyright: Copyright 2006-2017 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', 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'[()]', Text), |
|
194 (r',', Punctuation), |
|
195 ], |
|
196 'old_arguments': [ |
|
197 (r'\n', Text, '#pop'), |
|
198 |
|
199 include('variable_name'), |
|
200 include('operator'), |
|
201 include('number'), |
|
202 |
|
203 (r'"', String, 'string'), |
|
204 (r'[^\n]', Text), |
|
205 ], |
|
206 'number': [ |
|
207 (r'\n', Text, '#pop'), |
|
208 (r'\b\d+(\.\d*)?([eE][-+]?\d+)?%?', Number), |
|
209 ], |
|
210 'object_attributes': [ |
|
211 (r'\.?(n(col|row)|[xy]min|[xy]max|[nd][xy])\b', Name.Builtin, '#pop'), |
|
212 (r'(\.?(?:col|row)\$)(\[)', |
|
213 bygroups(Name.Builtin, Text), 'variable_name'), |
|
214 (r'(\$?)(\[)', |
|
215 bygroups(Name.Builtin, Text), ('#pop', 'comma_list')), |
|
216 ], |
|
217 'variable_name': [ |
|
218 include('operator'), |
|
219 include('number'), |
|
220 |
|
221 (words(variables_string, suffix=r'\$'), Name.Variable.Global), |
|
222 (words(variables_numeric, suffix=r'\b'), Name.Variable.Global), |
|
223 |
|
224 (r'\bObject_\w+', Name.Builtin, 'object_attributes'), |
|
225 (words(objects, prefix=r'\b', suffix=r'_\w+'), |
|
226 Name.Builtin, 'object_attributes'), |
|
227 |
|
228 (r"\b(Object_)(')", |
|
229 bygroups(Name.Builtin, String.Interpol), |
|
230 ('object_attributes', 'string_interpolated')), |
|
231 (words(objects, prefix=r'\b', suffix=r"(_)(')"), |
|
232 bygroups(Name.Builtin, Name.Builtin, String.Interpol), |
|
233 ('object_attributes', 'string_interpolated')), |
|
234 |
|
235 (r'\.?_?[a-z][\w.]*(\$|#)?', Text), |
|
236 (r'[\[\]]', Punctuation, 'comma_list'), |
|
237 (r"'(?=.*')", String.Interpol, 'string_interpolated'), |
|
238 ], |
|
239 'operator': [ |
|
240 (r'([+\/*<>=!-]=?|[&*|][&*|]?|\^|<>)', Operator), |
|
241 (r'(?<![\w.])(and|or|not|div|mod)(?![\w.])', Operator.Word), |
|
242 ], |
|
243 'string_interpolated': [ |
|
244 (r'\.?[_a-z][\w.]*[$#]?(?:\[[a-zA-Z0-9,]+\])?(:[0-9]+)?', |
|
245 String.Interpol), |
|
246 (r"'", String.Interpol, '#pop'), |
|
247 ], |
|
248 'string_unquoted': [ |
|
249 (r'(\n\s*)(\.{3})', bygroups(Text, Punctuation)), |
|
250 |
|
251 (r'\n', Text, '#pop'), |
|
252 (r'\s', Text), |
|
253 (r"'(?=.*')", String.Interpol, 'string_interpolated'), |
|
254 (r"'", String), |
|
255 (r"[^'\n]+", String), |
|
256 ], |
|
257 'string': [ |
|
258 (r'(\n\s*)(\.{3})', bygroups(Text, Punctuation)), |
|
259 |
|
260 (r'"', String, '#pop'), |
|
261 (r"'(?=.*')", String.Interpol, 'string_interpolated'), |
|
262 (r"'", String), |
|
263 (r'[^\'"\n]+', String), |
|
264 ], |
|
265 'old_form': [ |
|
266 (r'\s+', Text), |
|
267 |
|
268 (r'(optionmenu|choice)([ \t]+\S+:[ \t]+)', |
|
269 bygroups(Keyword, Text), 'number'), |
|
270 |
|
271 (r'(option|button)([ \t]+)', |
|
272 bygroups(Keyword, Text), 'string_unquoted'), |
|
273 |
|
274 (r'(sentence|text)([ \t]+\S+)', |
|
275 bygroups(Keyword, Text), 'string_unquoted'), |
|
276 |
|
277 (r'(word)([ \t]+\S+[ \t]*)(\S+)?([ \t]+.*)?', |
|
278 bygroups(Keyword, Text, String, Text)), |
|
279 |
|
280 (r'(boolean)(\s+\S+\s*)(0|1|"?(?:yes|no)"?)', |
|
281 bygroups(Keyword, Text, Name.Variable)), |
|
282 |
|
283 # Ideally processing of the number would happend in the 'number' |
|
284 # but that doesn't seem to work |
|
285 (r'(real|natural|positive|integer)([ \t]+\S+[ \t]*)([+-]?)(\d+(?:\.\d*)?' |
|
286 r'(?:[eE][-+]?\d+)?%?)', |
|
287 bygroups(Keyword, Text, Operator, Number)), |
|
288 |
|
289 (r'(comment)(\s+)', |
|
290 bygroups(Keyword, Text), 'string_unquoted'), |
|
291 |
|
292 (r'\bendform\b', Keyword, '#pop'), |
|
293 ] |
|
294 } |