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