rouge 3.19.0 → 3.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rouge.rb +1 -0
  3. data/lib/rouge/cli.rb +32 -2
  4. data/lib/rouge/demos/augeas +16 -0
  5. data/lib/rouge/demos/bibtex +12 -0
  6. data/lib/rouge/demos/brightscript +6 -0
  7. data/lib/rouge/demos/email +11 -0
  8. data/lib/rouge/demos/hlsl +20 -0
  9. data/lib/rouge/demos/j +12 -0
  10. data/lib/rouge/demos/janet +3 -0
  11. data/lib/rouge/demos/livescript +15 -0
  12. data/lib/rouge/demos/ocl +4 -0
  13. data/lib/rouge/demos/postscript +9 -0
  14. data/lib/rouge/demos/rescript +26 -0
  15. data/lib/rouge/demos/ssh +4 -0
  16. data/lib/rouge/demos/systemd +4 -0
  17. data/lib/rouge/demos/velocity +9 -0
  18. data/lib/rouge/demos/zig +6 -0
  19. data/lib/rouge/formatters/html_line_highlighter.rb +24 -0
  20. data/lib/rouge/formatters/html_line_table.rb +1 -3
  21. data/lib/rouge/formatters/html_linewise.rb +2 -3
  22. data/lib/rouge/lexer.rb +38 -20
  23. data/lib/rouge/lexers/apex.rb +9 -7
  24. data/lib/rouge/lexers/augeas.rb +93 -0
  25. data/lib/rouge/lexers/batchfile.rb +3 -2
  26. data/lib/rouge/lexers/bibtex.rb +115 -0
  27. data/lib/rouge/lexers/brightscript.rb +147 -0
  28. data/lib/rouge/lexers/cmake.rb +10 -0
  29. data/lib/rouge/lexers/cpp.rb +12 -5
  30. data/lib/rouge/lexers/crystal.rb +14 -9
  31. data/lib/rouge/lexers/css.rb +3 -1
  32. data/lib/rouge/lexers/diff.rb +1 -1
  33. data/lib/rouge/lexers/docker.rb +2 -2
  34. data/lib/rouge/lexers/elm.rb +5 -5
  35. data/lib/rouge/lexers/email.rb +39 -0
  36. data/lib/rouge/lexers/ghc_core.rb +2 -1
  37. data/lib/rouge/lexers/graphql.rb +1 -1
  38. data/lib/rouge/lexers/hack.rb +1 -1
  39. data/lib/rouge/lexers/haskell.rb +27 -19
  40. data/lib/rouge/lexers/hlsl.rb +166 -0
  41. data/lib/rouge/lexers/html.rb +7 -7
  42. data/lib/rouge/lexers/http.rb +8 -2
  43. data/lib/rouge/lexers/isbl.rb +2 -2
  44. data/lib/rouge/lexers/j.rb +244 -0
  45. data/lib/rouge/lexers/janet.rb +218 -0
  46. data/lib/rouge/lexers/javascript.rb +11 -5
  47. data/lib/rouge/lexers/jinja.rb +22 -7
  48. data/lib/rouge/lexers/jsl.rb +20 -8
  49. data/lib/rouge/lexers/jsonnet.rb +4 -3
  50. data/lib/rouge/lexers/jsp.rb +2 -3
  51. data/lib/rouge/lexers/jsx.rb +47 -59
  52. data/lib/rouge/lexers/julia.rb +4 -2
  53. data/lib/rouge/lexers/kotlin.rb +8 -4
  54. data/lib/rouge/lexers/livescript.rb +310 -0
  55. data/lib/rouge/lexers/ocaml/common.rb +1 -1
  56. data/lib/rouge/lexers/ocl.rb +85 -0
  57. data/lib/rouge/lexers/opentype_feature_file.rb +26 -42
  58. data/lib/rouge/lexers/perl.rb +27 -7
  59. data/lib/rouge/lexers/php.rb +274 -128
  60. data/lib/rouge/lexers/postscript.rb +93 -0
  61. data/lib/rouge/lexers/powershell.rb +44 -32
  62. data/lib/rouge/lexers/python.rb +1 -1
  63. data/lib/rouge/lexers/q.rb +1 -1
  64. data/lib/rouge/lexers/reasonml.rb +6 -5
  65. data/lib/rouge/lexers/rego.rb +27 -12
  66. data/lib/rouge/lexers/rescript.rb +119 -0
  67. data/lib/rouge/lexers/ruby.rb +1 -1
  68. data/lib/rouge/lexers/rust.rb +6 -3
  69. data/lib/rouge/lexers/sass/common.rb +1 -0
  70. data/lib/rouge/lexers/smarty.rb +1 -1
  71. data/lib/rouge/lexers/ssh.rb +33 -0
  72. data/lib/rouge/lexers/systemd.rb +34 -0
  73. data/lib/rouge/lexers/tsx.rb +10 -3
  74. data/lib/rouge/lexers/twig.rb +4 -4
  75. data/lib/rouge/lexers/typescript.rb +1 -12
  76. data/lib/rouge/lexers/typescript/common.rb +18 -4
  77. data/lib/rouge/lexers/velocity.rb +71 -0
  78. data/lib/rouge/lexers/wollok.rb +0 -1
  79. data/lib/rouge/lexers/xml.rb +5 -3
  80. data/lib/rouge/lexers/yaml.rb +5 -3
  81. data/lib/rouge/lexers/zig.rb +139 -0
  82. data/lib/rouge/regex_lexer.rb +56 -1
  83. data/lib/rouge/themes/base16.rb +1 -0
  84. data/lib/rouge/themes/bw.rb +1 -0
  85. data/lib/rouge/themes/colorful.rb +1 -0
  86. data/lib/rouge/themes/github.rb +1 -0
  87. data/lib/rouge/themes/gruvbox.rb +2 -0
  88. data/lib/rouge/themes/igor_pro.rb +1 -0
  89. data/lib/rouge/themes/magritte.rb +1 -0
  90. data/lib/rouge/themes/monokai.rb +1 -0
  91. data/lib/rouge/themes/pastie.rb +1 -0
  92. data/lib/rouge/themes/thankful_eyes.rb +1 -0
  93. data/lib/rouge/themes/tulip.rb +1 -0
  94. data/lib/rouge/version.rb +1 -1
  95. metadata +33 -2
@@ -3,7 +3,7 @@
3
3
 
4
4
  module Rouge
5
5
  module Lexers
6
- # shared states with Reasonml
6
+ # shared states with Reasonml and ReScript
7
7
  class OCamlCommon < RegexLexer
8
8
  def self.keywords
9
9
  @keywords ||= Set.new %w(
@@ -0,0 +1,85 @@
1
+ module Rouge
2
+ module Lexers
3
+ class OCL < RegexLexer
4
+ title "OCL"
5
+ desc "OMG Object Constraint Language (omg.org/spec/OCL)"
6
+ tag 'ocl'
7
+ aliases 'OCL'
8
+ filenames '*.ocl'
9
+ mimetypes 'text/x-ocl'
10
+
11
+ def self.keywords
12
+ @keywords ||= Set.new %w(
13
+ context pre post inv init body def derive if then else endif import
14
+ package endpackage let in
15
+ )
16
+ end
17
+
18
+ def self.keywords_type
19
+ @keywords_type ||= Set.new %w(
20
+ Boolean Integer UnlimitedNatural Real String OrderedSet Tuple Bag Set
21
+ Sequence OclInvalid OclVoid TupleType OclState Collection OclMessage
22
+ )
23
+ end
24
+
25
+ def self.builtins
26
+ @builtins ||= Set.new %w(
27
+ self null result true false invalid @pre
28
+ )
29
+ end
30
+
31
+ def self.operators
32
+ @operators ||= Set.new %w(
33
+ or xor and not implies
34
+ )
35
+ end
36
+
37
+ def self.functions
38
+ @functions ||= Set.new %w(
39
+ oclAsSet oclIsNew oclIsUndefined oclIsInvalid oclAsType oclIsTypeOf
40
+ oclIsKindOf oclInState oclType oclLocale hasReturned result
41
+ isSignalSent isOperationCallabs floor round max min toString div mod
42
+ size substring concat toInteger toReal toUpperCase toLowerCase
43
+ indexOf equalsIgnoreCase at characters toBoolean includes excludes
44
+ count includesAll excludesAll isEmpty notEmpty sum product
45
+ selectByKind selectByType asBag asSequence asOrderedSet asSet flatten
46
+ union intersection including excluding symmetricDifferencecount
47
+ append prepend insertAt subOrderedSet first last reverse subSequence
48
+ any closure collect collectNested exists forAll isUnique iterate one
49
+ reject select sortedBy allInstances average conformsTo
50
+ )
51
+ end
52
+
53
+ state :single_string do
54
+ rule %r/\\./, Str::Escape
55
+ rule %r/'/, Str::Single, :pop!
56
+ rule %r/[^\\']+/, Str::Single
57
+ end
58
+
59
+ state :root do
60
+ rule %r/\s+/m, Text
61
+ rule %r/--.*/, Comment::Single
62
+ rule %r/\d+/, Num::Integer
63
+ rule %r/'/, Str::Single, :single_string
64
+ rule %r([->|+*/<>=~!@#%&|?^-]), Operator
65
+ rule %r/[;:()\[\],.]/, Punctuation
66
+ rule %r/\w[\w\d]*/ do |m|
67
+ if self.class.operators.include? m[0]
68
+ token Operator
69
+ elsif self.class.keywords_type.include? m[0]
70
+ token Keyword::Declaration
71
+ elsif self.class.keywords.include? m[0]
72
+ token Keyword
73
+ elsif self.class.builtins.include? m[0]
74
+ token Name::Builtin
75
+ elsif self.class.functions.include? m[0]
76
+ token Name::Function
77
+ else
78
+ token Name
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
@@ -12,24 +12,30 @@ module Rouge
12
12
 
13
13
  def self.keywords
14
14
  @keywords ||= %w(
15
- Ascender Attach CapHeight CaretOffset CodePageRange Descender FontRevision FSType
16
- GlyphClassDef HorizAxis.BaseScriptList HorizAxis.BaseTagList HorizAxis.MinMax
17
- IgnoreBaseGlyphs IgnoreLigatures IgnoreMarks LigatureCaretByDev LigatureCaretByIndex
18
- LigatureCaretByPos LineGap MarkAttachClass MarkAttachmentType NULL Panose RightToLeft
19
- TypoAscender TypoDescender TypoLineGap UnicodeRange UseMarkFilteringSet Vendor
20
- VertAdvanceY VertAxis.BaseScriptList VertAxis.BaseTagList VertAxis.MinMax VertOriginY
21
- VertTypoAscender VertTypoDescender VertTypoLineGap WeightClass WidthClass XHeight
22
-
23
- anchorDef anchor anonymous anon by contour cursive device enumerate enum
24
- exclude_dflt featureNames feature from ignore include_dflt include languagesystem
25
- language lookupflag lookup markClass mark nameid name parameters position pos required
26
- reversesub rsub script sizemenuname substitute subtable sub table useExtension
15
+ Ascender Attach AxisValue CapHeight CaretOffset CodePageRange
16
+ DesignAxis Descender ElidedFallbackName ElidedFallbackNameID
17
+ ElidableAxisValueName FeatUILabelNameID FeatUITooltipTextNameID
18
+ FontRevision FSType GlyphClassDef HorizAxis.BaseScriptList
19
+ HorizAxis.BaseTagList HorizAxis.MinMax IgnoreBaseGlyphs
20
+ IgnoreLigatures IgnoreMarks LigatureCaretByDev LigatureCaretByIndex
21
+ LigatureCaretByPos LineGap MarkAttachClass MarkAttachmentType NULL
22
+ OlderSiblingFontAttribute Panose ParamUILabelNameID RightToLeft
23
+ SampleTextNameID TypoAscender TypoDescender TypoLineGap UnicodeRange
24
+ UseMarkFilteringSet Vendor VertAdvanceY VertAxis.BaseScriptList
25
+ VertAxis.BaseTagList VertAxis.MinMax VertOriginY VertTypoAscender
26
+ VertTypoDescender VertTypoLineGap WeightClass WidthClass XHeight
27
+
28
+ anchorDef anchor anonymous anon by contour cursive device enumerate
29
+ enum exclude_dflt featureNames feature flag from ignore include_dflt
30
+ include languagesystem language location lookupflag lookup markClass
31
+ mark nameid name parameters position pos required reversesub rsub
32
+ script sizemenuname substitute subtable sub table useExtension
27
33
  valueRecordDef winAscent winDescent
28
34
  )
29
35
  end
30
36
 
31
37
 
32
- identifier = %r/[a-z_][a-z0-9\/_]*/i
38
+ identifier = %r/[a-z_][a-z0-9\/_.-]*/i
33
39
 
34
40
  state :root do
35
41
  rule %r/\s+/m, Text::Whitespace
@@ -41,17 +47,18 @@ module Rouge
41
47
  push :featurename
42
48
  end
43
49
  # } <tag> ;
44
- rule %r/(\})((?:\s)*)/ do
50
+ rule %r/(\})((?:\s))/ do
45
51
  groups Punctuation, Text
46
52
  push :featurename
47
53
  end
48
54
  # solve include( ../path)
49
- rule %r/(include)/i, Keyword, :includepath
55
+ rule %r/include\b/i, Keyword, :includepath
50
56
 
51
57
  rule %r/[\-\[\]\/(){},.:;=%*<>']/, Punctuation
52
58
 
53
59
  rule %r/`.*?/, Str::Backtick
54
- rule %r/\"/, Str, :dqs
60
+ rule %r/\"/, Str, :strings
61
+ rule %r/\\[^.*\s]+/i, Str::Escape
55
62
 
56
63
  # classes, start with @<nameOfClass>
57
64
  rule %r/@#{identifier}/, Name::Class
@@ -66,6 +73,7 @@ module Rouge
66
73
  end
67
74
 
68
75
  rule identifier, Name
76
+ rule %r/(?:0x|\\)[0-9A-Fa-f]+/, Num::Hex
69
77
  rule %r/-?\d+/, Num::Integer
70
78
  end
71
79
 
@@ -77,37 +85,13 @@ module Rouge
77
85
  rule %r/\s+/, Text::Whitespace
78
86
  rule %r/\)/, Punctuation, :pop!
79
87
  rule %r/\(/, Punctuation
80
- rule %r/[a-z0-9\/_.]*/i, Str
88
+ rule %r/[^\s()]+/, Str
81
89
  end
82
90
 
83
91
  state :strings do
84
- rule %r/(\([a-z0-9_]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?/i, Str
85
- end
86
-
87
- state :strings_double do
88
- rule %r/[^\\"%\n]+/, Str
89
- mixin :strings
90
- end
91
-
92
- state :escape do
93
- rule %r(\\
94
- ( [\\abfnrtv"']
95
- | \n
96
- | N{[a-zA-z][a-zA-Z ]+[a-zA-Z]}
97
- | u[a-fA-F0-9]{4}
98
- | U[a-fA-F0-9]{8}
99
- | x[a-fA-F0-9]{2}
100
- | [0-7]{1,3}
101
- )
102
- )x, Str::Escape
103
- end
104
-
105
- state :dqs do
106
92
  rule %r/"/, Str, :pop!
107
- mixin :escape
108
- mixin :strings_double
93
+ rule %r/[^"%\n]+/, Str
109
94
  end
110
-
111
95
  end
112
96
  end
113
97
  end
@@ -65,7 +65,7 @@ module Rouge
65
65
  end
66
66
 
67
67
  state :root do
68
- rule %r/#.*?$/, Comment::Single
68
+ rule %r/#.*/, Comment::Single
69
69
  rule %r/^=[a-zA-Z0-9]+\s+.*?\n=cut/m, Comment::Multiline
70
70
  rule %r/(?:#{keywords.join('|')})\b/, Keyword
71
71
 
@@ -99,6 +99,9 @@ module Rouge
99
99
  re_tok, :balanced_regex
100
100
 
101
101
  rule %r/\s+/, Text
102
+
103
+ rule(/(?=[a-z_]\w*(\s*#.*\n)*\s*=>)/i) { push :fat_comma }
104
+
102
105
  rule %r/(?:#{builtins.join('|')})\b/, Name::Builtin
103
106
  rule %r/((__(DIE|WARN)__)|(DATA|STD(IN|OUT|ERR)))\b/,
104
107
  Name::Builtin::Pseudo
@@ -107,9 +110,10 @@ module Rouge
107
110
 
108
111
  rule %r/(__(END|DATA)__)\b/, Comment::Preproc, :end_part
109
112
  rule %r/\$\^[ADEFHILMOPSTWX]/, Name::Variable::Global
110
- rule %r/\$[\\"'\[\]&`+*.,;=%~?@$!<>(^\|\/-](?!\w)/, Name::Variable::Global
113
+ rule %r/\$[\\"'\[\]&`+*.,;=%~?@$!<>(^\|\/_-](?!\w)/, Name::Variable::Global
114
+ rule %r/[$@%&*][$@%&*#_]*(?=[a-z{\[;])/i, Name::Variable, :varname
115
+
111
116
  rule %r/[-+\/*%=<>&^\|!\\~]=?/, Operator
112
- rule %r/[$@%#]+/, Name::Variable, :varname
113
117
 
114
118
  rule %r/0_?[0-7]+(_[0-7]+)*/, Num::Oct
115
119
  rule %r/0x[0-9A-Fa-f]+(_[0-9A-Fa-f]+)*/, Num::Hex
@@ -142,6 +146,13 @@ module Rouge
142
146
  rule %r/.*?\n/, Str::Interpol
143
147
  end
144
148
 
149
+ state :fat_comma do
150
+ rule %r/#.*/, Comment::Single
151
+ rule %r/\w+/, Str
152
+ rule %r/\s+/, Text
153
+ rule %r/=>/, Operator, :pop!
154
+ end
155
+
145
156
  state :name_common do
146
157
  rule %r/\w+::/, Name::Namespace
147
158
  rule %r/[\w:]+/, Name::Variable, :pop!
@@ -149,8 +160,9 @@ module Rouge
149
160
 
150
161
  state :varname do
151
162
  rule %r/\s+/, Text
152
- rule %r/\{/, Punctuation, :pop! # hash syntax
153
- rule %r/\)|,/, Punctuation, :pop! # arg specifier
163
+ rule %r/[{\[]/, Punctuation, :pop! # hash syntax
164
+ rule %r/[),]/, Punctuation, :pop! # arg specifier
165
+ rule %r/[;]/, Punctuation, :pop! # postfix
154
166
  mixin :name_common
155
167
  end
156
168
 
@@ -178,16 +190,24 @@ module Rouge
178
190
  end
179
191
 
180
192
  state :sq do
181
- rule %r/\\[']/, Str::Escape
193
+ rule %r/\\[\\']/, Str::Escape
182
194
  rule %r/[^\\']+/, Str::Single
183
195
  rule %r/'/, Punctuation, :pop!
196
+ rule %r/\\/, Str::Single
184
197
  end
185
198
 
186
199
  state :dq do
187
200
  mixin :string_intp
188
- rule %r/\\[\\tnr"]/, Str::Escape
201
+ rule %r/\\[\\tnrabefluLUE"$@]/, Str::Escape
202
+ rule %r/\\0\d{2}/, Str::Escape
203
+ rule %r/\\o\{\d+\}/, Str::Escape
204
+ rule %r/\\x\h{2}/, Str::Escape
205
+ rule %r/\\x\{\h+\}/, Str::Escape
206
+ rule %r/\\c./, Str::Escape
207
+ rule %r/\\N\{[^\}]+\}/, Str::Escape
189
208
  rule %r/[^\\"]+?/, Str::Double
190
209
  rule %r/"/, Punctuation, :pop!
210
+ rule %r/\\/, Str::Escape
191
211
  end
192
212
 
193
213
  state :bq do
@@ -28,6 +28,26 @@ module Rouge
28
28
  @disabledmodules = list_option(:disabledmodules)
29
29
  end
30
30
 
31
+ def self.detect?(text)
32
+ return true if text.shebang?('php')
33
+ return false if /^<\?hh/ =~ text
34
+ return true if /^<\?php/ =~ text
35
+ end
36
+
37
+ def self.keywords
38
+ @keywords ||= Set.new %w(
39
+ old_function cfunction
40
+ __class__ __dir__ __file__ __function__ __halt_compiler __line__
41
+ __method__ __namespace__ __trait__ abstract and array as break case
42
+ catch clone continue declare default die do echo else elseif
43
+ enddeclare endfor endforeach endif endswitch endwhile eval exit
44
+ extends final finally fn for foreach global goto if implements
45
+ include include_once instanceof insteadof isset list new or parent
46
+ print private protected public require require_once return self
47
+ static switch throw try var while xor yield
48
+ )
49
+ end
50
+
31
51
  def self.builtins
32
52
  Kernel::load File.join(Lexers::BASE_DIR, 'php/keywords.rb')
33
53
  builtins
@@ -44,188 +64,314 @@ module Rouge
44
64
  end
45
65
  end
46
66
 
47
- # source: http://php.net/manual/en/language.variables.basics.php
48
- # the given regex is invalid utf8, so... we're using the unicode
49
- # "Letter" property instead.
50
67
  id = /[\p{L}_][\p{L}\p{N}_]*/
51
- nsid = /#{id}(?:\\#{id})*/
68
+ ns = /(?:#{id}\\)+/
69
+ id_with_ns = /(?:#{ns})?#{id}/
52
70
 
53
71
  start do
54
72
  case @start_inline
55
73
  when true
56
- push :template
57
74
  push :php
58
- when false
59
- push :template
60
75
  when :guess
61
- # pass
76
+ push :start
62
77
  end
63
78
  end
64
79
 
65
- def self.keywords
66
- # (echo parent ; echo self ; sed -nE 's/<ST_IN_SCRIPTING>"((__)?[[:alpha:]_]+(__)?)".*/\1/p' zend_language_scanner.l | tr '[A-Z]' '[a-z]') | sort -u | grep -Fwv -e isset -e unset -e empty -e const -e use -e function -e namespace
67
- # - isset, unset and empty are actually keywords (directly handled by PHP's lexer but let's pretend these are functions, you use them like so)
68
- # - self and parent are kind of keywords, they are not handled by PHP's lexer
69
- # - use, const, namespace and function are handled by specific rules to highlight what's next to the keyword
70
- # - class is also listed here, in addition to the rule below, to handle anonymous classes
71
- @keywords ||= Set.new %w(
72
- old_function cfunction
73
- __class__ __dir__ __file__ __function__ __halt_compiler
74
- __line__ __method__ __namespace__ __trait__ abstract and
75
- array as break callable case catch class clone continue
76
- declare default die do echo else elseif enddeclare
77
- endfor endforeach endif endswitch endwhile eval exit
78
- extends final finally fn for foreach global goto if
79
- implements include include_once instanceof insteadof
80
- interface list new or parent print private protected
81
- public require require_once return self static switch
82
- throw trait try var while xor yield
83
- )
80
+ state :escape do
81
+ rule %r/\?>/ do
82
+ token Comment::Preproc
83
+ reset_stack
84
+ end
84
85
  end
85
86
 
86
- def self.detect?(text)
87
- return true if text.shebang?('php')
88
- return false if /^<\?hh/ =~ text
89
- return true if /^<\?php/ =~ text
87
+ state :return do
88
+ rule(//) { pop! }
90
89
  end
91
90
 
92
- state :root do
93
- # some extremely rough heuristics to decide whether to start inline or not
94
- rule(/\s*(?=<)/m) { delegate parent; push :template }
95
- rule(/[^$]+(?=<\?(php|=))/i) { delegate parent; push :template }
91
+ state :start do
92
+ # We enter this state if we aren't sure whether the PHP in the text is
93
+ # delimited by <?php (or <?=) tags or not. These two rules check
94
+ # whether there is an opening angle bracket and, if there is, delegates
95
+ # the tokens before it to the HTML lexer.
96
+ rule(/\s*(?=<)/m) { delegate parent; pop! }
97
+ rule(/[^$]+(?=<\?(php|=))/i) { delegate parent; pop! }
96
98
 
97
- rule(//) { push :template; push :php }
99
+ rule(//) { goto :php }
98
100
  end
99
101
 
100
- state :template do
101
- rule %r/<\?(php|=)?/i, Comment::Preproc, :php
102
- rule(/.*?(?=<\?)|.*/m) { delegate parent }
103
- end
104
-
105
- state :php do
106
- rule %r/\?>/, Comment::Preproc, :pop!
107
- # heredocs
108
- rule %r/<<<(["']?)(#{id})\1\n.*?\n\s*\2;?/im, Str::Heredoc
109
- rule %r/\s+/, Text
110
- rule %r/#.*?$/, Comment::Single
111
- rule %r(//.*?$), Comment::Single
112
- rule %r(/\*\*(?!/).*?\*/)m, Comment::Doc
113
- rule %r(/\*.*?\*/)m, Comment::Multiline
114
-
115
- rule %r/(->|::)(\s*)(#{id})/ do
116
- groups Operator, Text, Name::Attribute
102
+ state :names do
103
+ rule %r/#{id_with_ns}(?=\s*\()/ do |m|
104
+ name = m[0].downcase
105
+ if self.class.keywords.include? name
106
+ token Keyword
107
+ elsif self.builtins.include? name
108
+ token Name::Builtin
109
+ else
110
+ token Name::Function
111
+ end
117
112
  end
118
113
 
119
- rule %r/(void|\??(int|float|bool|string|iterable|self|callable))\b/i, Keyword::Type
120
- rule %r/[~!%^&*+=\|:.<>\/@-]+/, Operator
121
- rule %r/\?/, Operator
122
- rule %r/[\[\]{}();,]/, Punctuation
123
- rule %r/(class|interface|trait)(\s+)(#{nsid})/i do
124
- groups Keyword::Declaration, Text, Name::Class
125
- end
126
- rule %r/(use)(\s+)(function|const|)(\s*)(#{nsid})/i do
127
- groups Keyword::Namespace, Text, Keyword::Namespace, Text, Name::Namespace
128
- push :use
129
- end
130
- rule %r/(namespace)(\s+)(#{nsid})/i do
131
- groups Keyword::Namespace, Text, Name::Namespace
132
- end
133
- # anonymous functions
134
- rule %r/(function)(\s*)(?=\()/i do
135
- groups Keyword, Text
114
+ rule id_with_ns do |m|
115
+ name = m[0].downcase
116
+ if name == "use"
117
+ push :in_use
118
+ token Keyword::Namespace
119
+ elsif name == "const"
120
+ push :in_const
121
+ token Keyword
122
+ elsif name == "catch"
123
+ push :in_catch
124
+ token Keyword
125
+ elsif %w(public protected private).include? name
126
+ push :in_visibility
127
+ token Keyword
128
+ elsif name == "stdClass"
129
+ token Name::Class
130
+ elsif self.class.keywords.include? name
131
+ token Keyword
132
+ elsif m[0] =~ /^__.*?__$/
133
+ token Name::Builtin
134
+ elsif m[0] =~ /^(E|PHP)(_[[:upper:]]+)+$/
135
+ token Keyword::Constant
136
+ elsif m[0] =~ /(\\|^)[[:upper:]][[[:upper:]][[:digit:]]_]+$/
137
+ token Name::Constant
138
+ elsif m[0] =~ /(\\|^)[[:upper:]][[:alnum:]]*?$/
139
+ token Name::Class
140
+ else
141
+ token Name
142
+ end
136
143
  end
144
+ end
137
145
 
138
- # named functions
139
- rule %r/(function)(\s+)(&?)(\s*)/i do
140
- groups Keyword, Text, Operator, Text
141
- push :funcname
142
- end
146
+ state :operators do
147
+ rule %r/[~!%^&*+\|:.<>\/@-]+/, Operator
148
+ end
143
149
 
144
- rule %r/(const)(\s+)(#{id})/i do
145
- groups Keyword, Text, Name::Constant
146
- end
150
+ state :string do
151
+ rule %r/"/, Str::Double, :pop!
152
+ rule %r/[^\\{$"]+/, Str::Double
153
+ rule %r/\\u\{[0-9a-fA-F]+\}/, Str::Escape
154
+ rule %r/\\([efrntv\"$\\]|[0-7]{1,3}|[xX][0-9a-fA-F]{1,2})/, Str::Escape
155
+ rule %r/\$#{id}(\[\S+\]|->#{id})?/, Name::Variable
147
156
 
148
- rule %r/stdClass\b/i, Name::Class
149
- rule %r/(true|false|null)\b/i, Keyword::Constant
150
- rule %r/(E|PHP)(_[[:upper:]]+)+\b/, Keyword::Constant
151
- rule %r/\$\{\$+#{id}\}/i, Name::Variable
152
- rule %r/\$+#{id}/i, Name::Variable
153
- rule %r/(yield)([ \n\r\t]+)(from)/i do
154
- groups Keyword, Text, Keyword
157
+ rule %r/\{\$\{/, Str::Interpol, :string_interp_double
158
+ rule %r/\{(?=\$)/, Str::Interpol, :string_interp_single
159
+ rule %r/(\{)(\S+)(\})/ do
160
+ groups Str::Interpol, Name::Variable, Str::Interpol
155
161
  end
156
162
 
157
- # may be intercepted for builtin highlighting
158
- rule %r/\\?#{nsid}/i do |m|
159
- name = m[0].downcase
163
+ rule %r/[${\\]+/, Str::Double
164
+ end
160
165
 
161
- if self.class.keywords.include? name
162
- token Keyword
163
- elsif self.builtins.include? name
164
- token Name::Builtin
165
- else
166
- token Name::Other
167
- end
168
- end
166
+ state :string_interp_double do
167
+ rule %r/\}\}/, Str::Interpol, :pop!
168
+ mixin :php
169
+ end
170
+
171
+ state :string_interp_single do
172
+ rule %r/\}/, Str::Interpol, :pop!
173
+ mixin :php
174
+ end
175
+
176
+ state :values do
177
+ # heredocs
178
+ rule %r/<<<(["']?)(#{id})\1\n.*?\n\s*\2;?/im, Str::Heredoc
169
179
 
180
+ # numbers
170
181
  rule %r/(\d[_\d]*)?\.(\d[_\d]*)?(e[+-]?\d[_\d]*)?/i, Num::Float
171
182
  rule %r/0[0-7][0-7_]*/, Num::Oct
172
183
  rule %r/0b[01][01_]*/i, Num::Bin
173
184
  rule %r/0x[a-f0-9][a-f0-9_]*/i, Num::Hex
174
185
  rule %r/\d[_\d]*/, Num::Integer
186
+
187
+ # strings
175
188
  rule %r/'([^'\\]*(?:\\.[^'\\]*)*)'/, Str::Single
176
189
  rule %r/`([^`\\]*(?:\\.[^`\\]*)*)`/, Str::Backtick
177
190
  rule %r/"/, Str::Double, :string
178
- end
179
191
 
180
- state :use do
181
- rule %r/(\s+)(as)(\s+)(#{id})/i do
182
- groups Text, Keyword, Text, Name
183
- :pop!
192
+ # functions
193
+ rule %r/(function|fn)\b/i do
194
+ push :in_function_return
195
+ push :in_function_params
196
+ push :in_function_name
197
+ token Keyword
184
198
  end
185
- rule %r/\\\{/, Operator, :uselist
186
- rule %r/;/, Punctuation, :pop!
199
+
200
+ # constants
201
+ rule %r/(true|false|null)\b/i, Keyword::Constant
202
+
203
+ # objects
204
+ rule %r/new\b/i, Keyword, :in_new
205
+ end
206
+
207
+ state :variables do
208
+ rule %r/\$\{\$+#{id}\}/, Name::Variable
209
+ rule %r/\$+#{id}/, Name::Variable
187
210
  end
188
211
 
189
- state :uselist do
212
+ state :whitespace do
190
213
  rule %r/\s+/, Text
191
- rule %r/,/, Operator
192
- rule %r/\}/, Operator, :pop!
193
- rule %r/(as)(\s+)(#{id})/i do
194
- groups Keyword, Text, Name
195
- end
196
- rule %r/#{id}/, Name::Namespace
214
+ rule %r/#.*?$/, Comment::Single
215
+ rule %r(//.*?$), Comment::Single
216
+ rule %r(/\*\*(?!/).*?\*/)m, Comment::Doc
217
+ rule %r(/\*.*?\*/)m, Comment::Multiline
197
218
  end
198
219
 
199
- state :funcname do
200
- rule %r/#{id}/, Name::Function, :pop!
220
+ state :root do
221
+ rule %r/<\?(php|=)?/i, Comment::Preproc, :php
222
+ rule(/.*?(?=<\?)|.*/m) { delegate parent }
201
223
  end
202
224
 
203
- state :string do
204
- rule %r/"/, Str::Double, :pop!
205
- rule %r/[^\\{$"]+/, Str::Double
206
- rule %r/\\u\{[0-9a-fA-F]+\}/, Str::Escape
207
- rule %r/\\([efrntv\"$\\]|[0-7]{1,3}|[xX][0-9a-fA-F]{1,2})/,
208
- Str::Escape
209
- rule %r/\$#{id}(\[\S+\]|->#{id})?/, Name::Variable
225
+ state :php do
226
+ mixin :escape
210
227
 
211
- rule %r/\{\$\{/, Str::Interpol, :interp_double
212
- rule %r/\{(?=\$)/, Str::Interpol, :interp_single
213
- rule %r/(\{)(\S+)(\})/ do
214
- groups Str::Interpol, Name::Variable, Str::Interpol
228
+ mixin :whitespace
229
+ mixin :variables
230
+ mixin :values
231
+
232
+ rule %r/(namespace)
233
+ (\s+)
234
+ (#{id_with_ns})/ix do |m|
235
+ groups Keyword::Namespace, Text, Name::Namespace
215
236
  end
216
237
 
217
- rule %r/[${\\]+/, Str::Double
238
+ rule %r/(class|interface|trait|extends|implements)
239
+ (\s+)
240
+ (#{id_with_ns})/ix do |m|
241
+ groups Keyword::Declaration, Text, Name::Class
242
+ end
243
+
244
+ mixin :names
245
+
246
+ rule %r/[;,\(\)\{\}\[\]]/, Punctuation
247
+
248
+ mixin :operators
249
+ rule %r/[=?]/, Operator
218
250
  end
219
251
 
220
- state :interp_double do
221
- rule %r/\}\}/, Str::Interpol, :pop!
252
+ state :in_assign do
253
+ rule %r/,/, Punctuation, :pop!
254
+ rule %r/[\[\]]/, Punctuation
255
+ rule %r/\(/, Punctuation, :in_assign_function
256
+ mixin :escape
257
+ mixin :whitespace
258
+ mixin :values
259
+ mixin :variables
260
+ mixin :names
261
+ mixin :operators
262
+ mixin :return
263
+ end
264
+
265
+ state :in_assign_function do
266
+ rule %r/\)/, Punctuation, :pop!
267
+ rule %r/,/, Punctuation
268
+ mixin :in_assign
269
+ end
270
+
271
+ state :in_catch do
272
+ rule %r/\(/, Punctuation
273
+ rule %r/\|/, Operator
274
+ rule id, Name::Class
275
+ mixin :escape
276
+ mixin :whitespace
277
+ mixin :return
278
+ end
279
+
280
+ state :in_const do
281
+ rule id, Name::Constant
282
+ rule %r/=/, Operator, :in_assign
283
+ mixin :escape
284
+ mixin :whitespace
285
+ mixin :return
286
+ end
287
+
288
+ state :in_function_body do
289
+ rule %r/{/, Punctuation, :push
290
+ rule %r/}/, Punctuation, :pop!
222
291
  mixin :php
223
292
  end
224
293
 
225
- state :interp_single do
226
- rule %r/\}/, Str::Interpol, :pop!
294
+ state :in_function_name do
295
+ rule %r/&/, Operator
296
+ rule id, Name
297
+ rule %r/\(/, Punctuation, :pop!
298
+ mixin :escape
299
+ mixin :whitespace
300
+ mixin :return
301
+ end
302
+
303
+ state :in_function_params do
304
+ rule %r/\)/, Punctuation, :pop!
305
+ rule %r/,/, Punctuation
306
+ rule %r/[.]{3}/, Punctuation
307
+ rule %r/=/, Operator, :in_assign
308
+ rule %r/\??#{id}/, Keyword::Type, :in_assign
309
+ mixin :escape
310
+ mixin :whitespace
311
+ mixin :variables
312
+ mixin :return
313
+ end
314
+
315
+ state :in_function_return do
316
+ rule %r/:/, Punctuation
317
+ rule %r/use\b/i, Keyword, :in_function_use
318
+ rule %r/\??#{id}/, Keyword::Type, :in_assign
319
+ rule %r/\{/ do
320
+ token Punctuation
321
+ goto :in_function_body
322
+ end
323
+ mixin :escape
324
+ mixin :whitespace
325
+ mixin :return
326
+ end
327
+
328
+ state :in_function_use do
329
+ rule %r/[,\(]/, Punctuation
330
+ rule %r/&/, Operator
331
+ rule %r/\)/, Punctuation, :pop!
332
+ mixin :escape
333
+ mixin :whitespace
334
+ mixin :variables
335
+ mixin :return
336
+ end
337
+
338
+ state :in_new do
339
+ rule %r/class\b/i do
340
+ token Keyword::Declaration
341
+ goto :in_new_class
342
+ end
343
+ rule id_with_ns, Name::Class, :pop!
344
+ mixin :escape
345
+ mixin :whitespace
346
+ mixin :return
347
+ end
348
+
349
+ state :in_new_class do
350
+ rule %r/\}/, Punctuation, :pop!
351
+ rule %r/\{/, Punctuation
227
352
  mixin :php
228
353
  end
354
+
355
+ state :in_use do
356
+ rule %r/[,\}]/, Punctuation
357
+ rule %r/(function|const)\b/i, Keyword
358
+ rule %r/(#{ns})(\{)/ do
359
+ groups Name::Namespace, Punctuation
360
+ end
361
+ rule %r/#{id_with_ns}(_#{id})+/, Name::Function
362
+ mixin :escape
363
+ mixin :whitespace
364
+ mixin :names
365
+ mixin :return
366
+ end
367
+
368
+ state :in_visibility do
369
+ rule %r/(?=(abstract|const|function|static)\b)/i, Keyword, :pop!
370
+ rule %r/\??#{id}/, Keyword::Type, :pop!
371
+ mixin :escape
372
+ mixin :whitespace
373
+ mixin :return
374
+ end
229
375
  end
230
376
  end
231
377
  end