praat.py 12 KB

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