praat.py 12 KB

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