gitlab-rouge 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +23 -0
  3. data/LICENSE +186 -0
  4. data/bin/rougify +16 -0
  5. data/gitlab-rouge.gemspec +17 -0
  6. data/lib/rouge.rb +57 -0
  7. data/lib/rouge/cli.rb +363 -0
  8. data/lib/rouge/demos/apache +21 -0
  9. data/lib/rouge/demos/applescript +2 -0
  10. data/lib/rouge/demos/c +8 -0
  11. data/lib/rouge/demos/clojure +5 -0
  12. data/lib/rouge/demos/coffeescript +5 -0
  13. data/lib/rouge/demos/common_lisp +1 -0
  14. data/lib/rouge/demos/conf +4 -0
  15. data/lib/rouge/demos/cpp +8 -0
  16. data/lib/rouge/demos/csharp +5 -0
  17. data/lib/rouge/demos/css +4 -0
  18. data/lib/rouge/demos/dart +6 -0
  19. data/lib/rouge/demos/diff +7 -0
  20. data/lib/rouge/demos/elixir +1 -0
  21. data/lib/rouge/demos/erb +1 -0
  22. data/lib/rouge/demos/erlang +7 -0
  23. data/lib/rouge/demos/factor +5 -0
  24. data/lib/rouge/demos/gherkin +17 -0
  25. data/lib/rouge/demos/glsl +14 -0
  26. data/lib/rouge/demos/go +7 -0
  27. data/lib/rouge/demos/groovy +9 -0
  28. data/lib/rouge/demos/haml +5 -0
  29. data/lib/rouge/demos/handlebars +7 -0
  30. data/lib/rouge/demos/haskell +6 -0
  31. data/lib/rouge/demos/html +8 -0
  32. data/lib/rouge/demos/http +14 -0
  33. data/lib/rouge/demos/ini +4 -0
  34. data/lib/rouge/demos/io +11 -0
  35. data/lib/rouge/demos/java +5 -0
  36. data/lib/rouge/demos/javascript +1 -0
  37. data/lib/rouge/demos/json +1 -0
  38. data/lib/rouge/demos/json-doc +1 -0
  39. data/lib/rouge/demos/liquid +11 -0
  40. data/lib/rouge/demos/literate_coffeescript +3 -0
  41. data/lib/rouge/demos/literate_haskell +7 -0
  42. data/lib/rouge/demos/llvm +20 -0
  43. data/lib/rouge/demos/lua +12 -0
  44. data/lib/rouge/demos/make +6 -0
  45. data/lib/rouge/demos/markdown +4 -0
  46. data/lib/rouge/demos/matlab +6 -0
  47. data/lib/rouge/demos/moonscript +16 -0
  48. data/lib/rouge/demos/nginx +5 -0
  49. data/lib/rouge/demos/nim +27 -0
  50. data/lib/rouge/demos/objective_c +14 -0
  51. data/lib/rouge/demos/ocaml +12 -0
  52. data/lib/rouge/demos/perl +5 -0
  53. data/lib/rouge/demos/php +3 -0
  54. data/lib/rouge/demos/plaintext +1 -0
  55. data/lib/rouge/demos/powershell +49 -0
  56. data/lib/rouge/demos/prolog +9 -0
  57. data/lib/rouge/demos/properties +7 -0
  58. data/lib/rouge/demos/puppet +6 -0
  59. data/lib/rouge/demos/python +6 -0
  60. data/lib/rouge/demos/qml +9 -0
  61. data/lib/rouge/demos/r +8 -0
  62. data/lib/rouge/demos/racket +24 -0
  63. data/lib/rouge/demos/ruby +9 -0
  64. data/lib/rouge/demos/rust +12 -0
  65. data/lib/rouge/demos/sass +3 -0
  66. data/lib/rouge/demos/scala +3 -0
  67. data/lib/rouge/demos/scheme +4 -0
  68. data/lib/rouge/demos/scss +5 -0
  69. data/lib/rouge/demos/sed +4 -0
  70. data/lib/rouge/demos/shell +2 -0
  71. data/lib/rouge/demos/slim +17 -0
  72. data/lib/rouge/demos/smalltalk +6 -0
  73. data/lib/rouge/demos/sml +4 -0
  74. data/lib/rouge/demos/sql +1 -0
  75. data/lib/rouge/demos/swift +5 -0
  76. data/lib/rouge/demos/tcl +1 -0
  77. data/lib/rouge/demos/tex +1 -0
  78. data/lib/rouge/demos/toml +9 -0
  79. data/lib/rouge/demos/tulip +14 -0
  80. data/lib/rouge/demos/vb +4 -0
  81. data/lib/rouge/demos/viml +5 -0
  82. data/lib/rouge/demos/xml +2 -0
  83. data/lib/rouge/demos/yaml +4 -0
  84. data/lib/rouge/formatter.rb +50 -0
  85. data/lib/rouge/formatters/html.rb +117 -0
  86. data/lib/rouge/formatters/null.rb +19 -0
  87. data/lib/rouge/formatters/terminal256.rb +176 -0
  88. data/lib/rouge/lexer.rb +443 -0
  89. data/lib/rouge/lexers/apache.rb +68 -0
  90. data/lib/rouge/lexers/apache/keywords.yml +453 -0
  91. data/lib/rouge/lexers/apple_script.rb +367 -0
  92. data/lib/rouge/lexers/c.rb +212 -0
  93. data/lib/rouge/lexers/clojure.rb +112 -0
  94. data/lib/rouge/lexers/coffeescript.rb +174 -0
  95. data/lib/rouge/lexers/common_lisp.rb +345 -0
  96. data/lib/rouge/lexers/conf.rb +24 -0
  97. data/lib/rouge/lexers/cpp.rb +66 -0
  98. data/lib/rouge/lexers/csharp.rb +88 -0
  99. data/lib/rouge/lexers/css.rb +271 -0
  100. data/lib/rouge/lexers/dart.rb +104 -0
  101. data/lib/rouge/lexers/diff.rb +31 -0
  102. data/lib/rouge/lexers/elixir.rb +108 -0
  103. data/lib/rouge/lexers/erb.rb +56 -0
  104. data/lib/rouge/lexers/erlang.rb +118 -0
  105. data/lib/rouge/lexers/factor.rb +302 -0
  106. data/lib/rouge/lexers/gherkin.rb +137 -0
  107. data/lib/rouge/lexers/gherkin/keywords.rb +14 -0
  108. data/lib/rouge/lexers/glsl.rb +135 -0
  109. data/lib/rouge/lexers/go.rb +178 -0
  110. data/lib/rouge/lexers/groovy.rb +104 -0
  111. data/lib/rouge/lexers/haml.rb +228 -0
  112. data/lib/rouge/lexers/handlebars.rb +79 -0
  113. data/lib/rouge/lexers/haskell.rb +183 -0
  114. data/lib/rouge/lexers/html.rb +94 -0
  115. data/lib/rouge/lexers/http.rb +80 -0
  116. data/lib/rouge/lexers/ini.rb +57 -0
  117. data/lib/rouge/lexers/io.rb +68 -0
  118. data/lib/rouge/lexers/java.rb +76 -0
  119. data/lib/rouge/lexers/javascript.rb +297 -0
  120. data/lib/rouge/lexers/liquid.rb +287 -0
  121. data/lib/rouge/lexers/literate_coffeescript.rb +33 -0
  122. data/lib/rouge/lexers/literate_haskell.rb +36 -0
  123. data/lib/rouge/lexers/llvm.rb +84 -0
  124. data/lib/rouge/lexers/lua.rb +122 -0
  125. data/lib/rouge/lexers/lua/builtins.rb +22 -0
  126. data/lib/rouge/lexers/make.rb +116 -0
  127. data/lib/rouge/lexers/markdown.rb +154 -0
  128. data/lib/rouge/lexers/matlab.rb +74 -0
  129. data/lib/rouge/lexers/matlab/builtins.rb +11 -0
  130. data/lib/rouge/lexers/moonscript.rb +110 -0
  131. data/lib/rouge/lexers/nginx.rb +71 -0
  132. data/lib/rouge/lexers/nim.rb +152 -0
  133. data/lib/rouge/lexers/objective_c.rb +197 -0
  134. data/lib/rouge/lexers/ocaml.rb +111 -0
  135. data/lib/rouge/lexers/perl.rb +197 -0
  136. data/lib/rouge/lexers/php.rb +173 -0
  137. data/lib/rouge/lexers/php/builtins.rb +204 -0
  138. data/lib/rouge/lexers/plain_text.rb +25 -0
  139. data/lib/rouge/lexers/powershell.rb +96 -0
  140. data/lib/rouge/lexers/prolog.rb +64 -0
  141. data/lib/rouge/lexers/properties.rb +55 -0
  142. data/lib/rouge/lexers/puppet.rb +128 -0
  143. data/lib/rouge/lexers/python.rb +228 -0
  144. data/lib/rouge/lexers/qml.rb +72 -0
  145. data/lib/rouge/lexers/r.rb +56 -0
  146. data/lib/rouge/lexers/racket.rb +542 -0
  147. data/lib/rouge/lexers/ruby.rb +415 -0
  148. data/lib/rouge/lexers/rust.rb +191 -0
  149. data/lib/rouge/lexers/sass.rb +74 -0
  150. data/lib/rouge/lexers/sass/common.rb +180 -0
  151. data/lib/rouge/lexers/scala.rb +142 -0
  152. data/lib/rouge/lexers/scheme.rb +112 -0
  153. data/lib/rouge/lexers/scss.rb +34 -0
  154. data/lib/rouge/lexers/sed.rb +170 -0
  155. data/lib/rouge/lexers/shell.rb +152 -0
  156. data/lib/rouge/lexers/slim.rb +228 -0
  157. data/lib/rouge/lexers/smalltalk.rb +116 -0
  158. data/lib/rouge/lexers/sml.rb +347 -0
  159. data/lib/rouge/lexers/sql.rb +140 -0
  160. data/lib/rouge/lexers/swift.rb +144 -0
  161. data/lib/rouge/lexers/tcl.rb +192 -0
  162. data/lib/rouge/lexers/tex.rb +72 -0
  163. data/lib/rouge/lexers/toml.rb +71 -0
  164. data/lib/rouge/lexers/tulip.rb +75 -0
  165. data/lib/rouge/lexers/vb.rb +164 -0
  166. data/lib/rouge/lexers/viml.rb +101 -0
  167. data/lib/rouge/lexers/viml/keywords.rb +12 -0
  168. data/lib/rouge/lexers/xml.rb +59 -0
  169. data/lib/rouge/lexers/yaml.rb +364 -0
  170. data/lib/rouge/plugins/redcarpet.rb +30 -0
  171. data/lib/rouge/regex_lexer.rb +439 -0
  172. data/lib/rouge/template_lexer.rb +22 -0
  173. data/lib/rouge/text_analyzer.rb +48 -0
  174. data/lib/rouge/theme.rb +195 -0
  175. data/lib/rouge/themes/base16.rb +130 -0
  176. data/lib/rouge/themes/colorful.rb +67 -0
  177. data/lib/rouge/themes/github.rb +71 -0
  178. data/lib/rouge/themes/molokai.rb +82 -0
  179. data/lib/rouge/themes/monokai.rb +92 -0
  180. data/lib/rouge/themes/monokai_sublime.rb +90 -0
  181. data/lib/rouge/themes/thankful_eyes.rb +71 -0
  182. data/lib/rouge/token.rb +182 -0
  183. data/lib/rouge/util.rb +101 -0
  184. data/lib/rouge/version.rb +7 -0
  185. metadata +231 -0
@@ -0,0 +1,104 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class Dart < RegexLexer
6
+ title "Dart"
7
+ desc "The Dart programming language (dartlang.com)"
8
+
9
+ tag 'dart'
10
+ filenames '*.dart'
11
+ mimetypes 'text/x-dart'
12
+
13
+ keywords = %w(
14
+ as assert break case catch continue default do else finally for
15
+ if in is new rethrow return super switch this throw try while with
16
+ )
17
+
18
+ declarations = %w(
19
+ abstract dynamic const external extends factory final get implements
20
+ native operator set static typedef var
21
+ )
22
+
23
+ types = %w(bool double Dynamic enum int num Object Set String void)
24
+
25
+ imports = %w(import export library part\s*of part source)
26
+
27
+ id = /[a-zA-Z_]\w*/
28
+
29
+ state :root do
30
+ rule %r(^
31
+ (\s*(?:[a-zA-Z_][a-zA-Z\d_.\[\]]*\s+)+?) # return arguments
32
+ ([a-zA-Z_][\w]*) # method name
33
+ (\s*)(\() # signature start
34
+ )mx do |m|
35
+ # TODO: do this better, this shouldn't need a delegation
36
+ delegate Dart, m[1]
37
+ token Name::Function, m[2]
38
+ token Text, m[3]
39
+ token Punctuation, m[4]
40
+ end
41
+
42
+ rule /\s+/, Text
43
+ rule %r(//.*?$), Comment::Single
44
+ rule %r(/\*.*?\*/)m, Comment::Multiline
45
+ rule /"/, Str, :dqs
46
+ rule /'/, Str, :sqs
47
+ rule /r"[^"]*"/, Str::Other
48
+ rule /r'[^']*'/, Str::Other
49
+ rule /##{id}*/i, Str::Symbol
50
+ rule /@#{id}/, Name::Decorator
51
+ rule /(?:#{keywords.join('|')})\b/, Keyword
52
+ rule /(?:#{declarations.join('|')})\b/, Keyword::Declaration
53
+ rule /(?:#{types.join('|')})\b/, Keyword::Type
54
+ rule /(?:true|false|null)\b/, Keyword::Constant
55
+ rule /(?:class|interface)\b/, Keyword::Declaration, :class
56
+ rule /(?:#{imports.join('|')})\b/, Keyword::Namespace, :import
57
+ rule /(\.)(#{id})/ do
58
+ groups Operator, Name::Attribute
59
+ end
60
+
61
+ rule /#{id}:/, Name::Label
62
+ rule /\$?#{id}/, Name
63
+ rule /[~^*!%&\[\](){}<>\|+=:;,.\/?-]/, Operator
64
+ rule /\d*\.\d+([eE]\-?\d+)?/, Num::Float
65
+ rule /0x[\da-fA-F]+/, Num::Hex
66
+ rule /\d+L?/, Num::Integer
67
+ rule /\n/, Text
68
+ end
69
+
70
+ state :class do
71
+ rule /\s+/m, Text
72
+ rule id, Name::Class, :pop!
73
+ end
74
+
75
+ state :dqs do
76
+ rule /"/, Str, :pop!
77
+ rule /[^\\\$"]+/, Str
78
+ mixin :string
79
+ end
80
+
81
+ state :sqs do
82
+ rule /'/, Str, :pop!
83
+ rule /[^\\\$']+/, Str
84
+ mixin :string
85
+ end
86
+
87
+ state :import do
88
+ rule /;/, Operator, :pop!
89
+ rule /(?:show|hide)\b/, Keyword::Declaration
90
+ mixin :root
91
+ end
92
+
93
+ state :string do
94
+ mixin :interpolation
95
+ rule /\\[nrt\"\'\\]/, Str::Escape
96
+ end
97
+
98
+ state :interpolation do
99
+ rule /\$#{id}/, Str::Interpol
100
+ rule /\$\{[^\}]+\}/, Str::Interpol
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,31 @@
1
+ module Rouge
2
+ module Lexers
3
+ class Diff < RegexLexer
4
+ title 'diff'
5
+ desc 'Lexes unified diffs or patches'
6
+
7
+ tag 'diff'
8
+ aliases 'patch', 'udiff'
9
+ filenames '*.diff', '*.patch'
10
+ mimetypes 'text/x-diff', 'text/x-patch'
11
+
12
+ def self.analyze_text(text)
13
+ return 1 if text.start_with?('Index: ')
14
+ return 1 if text.start_with?('diff ')
15
+ return 0.9 if text.start_with?('--- ')
16
+ end
17
+
18
+ state :root do
19
+ rule(/^ .*\n/, Text)
20
+ rule(/^---\n/, Text)
21
+ rule(/^\+.*\n/, Generic::Inserted)
22
+ rule(/^-+.*\n/, Generic::Deleted)
23
+ rule(/^!.*\n/, Generic::Strong)
24
+ rule(/^@.*\n/, Generic::Subheading)
25
+ rule(/^([Ii]ndex|diff).*\n/, Generic::Heading)
26
+ rule(/^=.*\n/, Generic::Heading)
27
+ rule(/.*\n/, Text)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,108 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ # Direct port of pygments Lexer.
6
+ # See: https://bitbucket.org/birkenfeld/pygments-main/src/7304e4759ae65343d89a51359ca538912519cc31/pygments/lexers/functional.py?at=default#cl-2362
7
+ class Elixir < RegexLexer
8
+ title "Elixir"
9
+ desc "Elixir language (elixir-lang.org)"
10
+
11
+ tag 'elixir'
12
+ aliases 'elixir', 'exs'
13
+
14
+ filenames '*.ex', '*.exs'
15
+
16
+ mimetypes 'text/x-elixir', 'application/x-elixir'
17
+
18
+ BRACES = [
19
+ ['\{', '\}', 'cb'],
20
+ ['\[', '\]', 'sb'],
21
+ ['\(', '\)', 'pa'],
22
+ ['\<', '\>', 'lt']
23
+ ]
24
+
25
+ state :root do
26
+ rule /\s+/m, Text
27
+ rule /#.*$/, Comment::Single
28
+ rule %r{\b(case|cond|end|bc|lc|if|unless|try|loop|receive|fn|defmodule|
29
+ defp?|defprotocol|defimpl|defrecord|defmacrop?|defdelegate|
30
+ defexception|exit|raise|throw|unless|after|rescue|catch|else)\b(?![?!])|
31
+ (?<!\.)\b(do|\-\>)\b}x, Keyword
32
+ rule /\b(import|require|use|recur|quote|unquote|super|refer)\b(?![?!])/, Keyword::Namespace
33
+ rule /(?<!\.)\b(and|not|or|when|xor|in)\b/, Operator::Word
34
+ rule %r{%=|\*=|\*\*=|\+=|\-=|\^=|\|\|=|
35
+ <=>|<(?!<|=)|>(?!<|=|>)|<=|>=|===|==|=~|!=|!~|(?=[\s\t])\?|
36
+ (?<=[\s\t])!+|&(&&?|(?!\d))|\|\||\^|\*|\+|\-|/|
37
+ \||\+\+|\-\-|\*\*|\/\/|\<\-|\<\>|<<|>>|=|\.|~~~}x, Operator
38
+ rule %r{(?<!:)(:)([a-zA-Z_]\w*([?!]|=(?![>=]))?|\<\>|===?|>=?|<=?|
39
+ <=>|&&?|%\(\)|%\[\]|%\{\}|\+\+?|\-\-?|\|\|?|\!|//|[%&`/\|]|
40
+ \*\*?|=?~|<\-)|([a-zA-Z_]\w*([?!])?)(:)(?!:)}, Str::Symbol
41
+ rule /:"/, Str::Symbol, :interpoling_symbol
42
+ rule /\b(nil|true|false)\b(?![?!])|\b[A-Z]\w*\b/, Name::Constant
43
+ rule /\b(__(FILE|LINE|MODULE|MAIN|FUNCTION)__)\b(?![?!])/, Name::Builtin::Pseudo
44
+ rule /[a-zA-Z_!][\w_]*[!\?]?/, Name
45
+ rule %r{::|[%(){};,/\|:\\\[\]]}, Punctuation
46
+ rule /@[a-zA-Z_]\w*|&\d/, Name::Variable
47
+ rule %r{\b(0[xX][0-9A-Fa-f]+|\d(_?\d)*(\.(?![^\d\s])
48
+ (_?\d)*)?([eE][-+]?\d(_?\d)*)?|0[bB][01]+)\b}x, Num
49
+ rule %r{~r\/.*\/}, Str::Regex
50
+
51
+ mixin :strings
52
+ end
53
+
54
+ state :strings do
55
+ rule /(%[A-Ba-z])?"""(?:.|\n)*?"""/, Str::Doc
56
+ rule /'''(?:.|\n)*?'''/, Str::Doc
57
+ rule /"/, Str::Doc, :dqs
58
+ rule /'.*?'/, Str::Single
59
+ rule %r{(?<!\w)\?(\\(x\d{1,2}|\h{1,2}(?!\h)\b|0[0-7]{0,2}(?![0-7])\b[^x0MC])|(\\[MC]-)+\w|[^\s\\])}, Str::Other
60
+
61
+ BRACES.each do |_, _, name|
62
+ mixin :"braces_#{name}"
63
+ end
64
+ end
65
+
66
+ BRACES.each do |lbrace, rbrace, name|
67
+ state :"braces_#{name}" do
68
+ rule /%[a-z]#{lbrace}/, Str::Double, :"braces_#{name}_intp"
69
+ rule /%[A-Z]#{lbrace}/, Str::Double, :"braces_#{name}_no_intp"
70
+ end
71
+
72
+ state :"braces_#{name}_intp" do
73
+ rule /#{rbrace}[a-z]*/, Str::Double, :pop!
74
+ mixin :enddoublestr
75
+ end
76
+
77
+ state :"braces_#{name}_no_intp" do
78
+ rule /.*#{rbrace}[a-z]*/, Str::Double, :pop!
79
+ end
80
+ end
81
+
82
+ state :dqs do
83
+ rule /"/, Str::Double, :pop!
84
+ mixin :enddoublestr
85
+ end
86
+
87
+ state :interpoling do
88
+ rule /#\{/, Str::Interpol, :interpoling_string
89
+ end
90
+
91
+ state :interpoling_string do
92
+ rule /\}/, Str::Interpol, :pop!
93
+ mixin :root
94
+ end
95
+
96
+ state :interpoling_symbol do
97
+ rule /"/, Str::Symbol, :pop!
98
+ mixin :interpoling
99
+ rule /[^#"]+/, Str::Symbol
100
+ end
101
+
102
+ state :enddoublestr do
103
+ mixin :interpoling
104
+ rule /[^#"]+/, Str::Double
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class ERB < TemplateLexer
6
+ title "ERB"
7
+ desc "Embedded ruby template files"
8
+
9
+ tag 'erb'
10
+ aliases 'eruby', 'rhtml'
11
+
12
+ filenames '*.erb', '*.erubis', '*.rhtml', '*.eruby'
13
+
14
+ def self.analyze_text(text)
15
+ return 0.4 if text =~ /<%.*%>/
16
+ end
17
+
18
+ def initialize(opts={})
19
+ @ruby_lexer = Ruby.new(opts)
20
+
21
+ super(opts)
22
+ end
23
+
24
+ start do
25
+ parent.reset!
26
+ @ruby_lexer.reset!
27
+ end
28
+
29
+ open = /<%%|<%=|<%#|<%-|<%/
30
+ close = /%%>|-%>|%>/
31
+
32
+ state :root do
33
+ rule /<%#/, Comment, :comment
34
+
35
+ rule open, Comment::Preproc, :ruby
36
+
37
+ rule /.+?(?=#{open})|.+/m do
38
+ delegate parent
39
+ end
40
+ end
41
+
42
+ state :comment do
43
+ rule close, Comment, :pop!
44
+ rule /.+(?=#{close})|.+/m, Comment
45
+ end
46
+
47
+ state :ruby do
48
+ rule close, Comment::Preproc, :pop!
49
+
50
+ rule /.+?(?=#{close})|.+/m do
51
+ delegate @ruby_lexer
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,118 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class Erlang < RegexLexer
6
+ title "Erlang"
7
+ desc "The Erlang programming language (erlang.org)"
8
+ tag 'erlang'
9
+ aliases 'erl'
10
+ filenames '*.erl', '*.hrl'
11
+
12
+ mimetypes 'text/x-erlang', 'application/x-erlang'
13
+
14
+ def self.analyze_text(text)
15
+ return 0.3 if text =~ /^-module[(]\w+[)][.]/
16
+ end
17
+
18
+ keywords = %w(
19
+ after begin case catch cond end fun if
20
+ let of query receive try when
21
+ )
22
+
23
+ builtins = %w(
24
+ abs append_element apply atom_to_list binary_to_list
25
+ bitstring_to_list binary_to_term bit_size bump_reductions
26
+ byte_size cancel_timer check_process_code delete_module
27
+ demonitor disconnect_node display element erase exit
28
+ float float_to_list fun_info fun_to_list
29
+ function_exported garbage_collect get get_keys
30
+ group_leader hash hd integer_to_list iolist_to_binary
31
+ iolist_size is_atom is_binary is_bitstring is_boolean
32
+ is_builtin is_float is_function is_integer is_list
33
+ is_number is_pid is_port is_process_alive is_record
34
+ is_reference is_tuple length link list_to_atom
35
+ list_to_binary list_to_bitstring list_to_existing_atom
36
+ list_to_float list_to_integer list_to_pid list_to_tuple
37
+ load_module localtime_to_universaltime make_tuple md5
38
+ md5_final md5_update memory module_loaded monitor
39
+ monitor_node node nodes open_port phash phash2
40
+ pid_to_list port_close port_command port_connect
41
+ port_control port_call port_info port_to_list
42
+ process_display process_flag process_info purge_module
43
+ put read_timer ref_to_list register resume_process
44
+ round send send_after send_nosuspend set_cookie
45
+ setelement size spawn spawn_link spawn_monitor
46
+ spawn_opt split_binary start_timer statistics
47
+ suspend_process system_flag system_info system_monitor
48
+ system_profile term_to_binary tl trace trace_delivered
49
+ trace_info trace_pattern trunc tuple_size tuple_to_list
50
+ universaltime_to_localtime unlink unregister whereis
51
+ )
52
+
53
+ operators = %r{(\+\+?|--?|\*|/|<|>|/=|=:=|=/=|=<|>=|==?|<-|!|\?)}
54
+ word_operators = %w(
55
+ and andalso band bnot bor bsl bsr bxor
56
+ div not or orelse rem xor
57
+ )
58
+
59
+ atom_re = %r{(?:[a-z][a-zA-Z0-9_]*|'[^\n']*[^\\]')}
60
+
61
+ variable_re = %r{(?:[A-Z_][a-zA-Z0-9_]*)}
62
+
63
+ escape_re = %r{(?:\\(?:[bdefnrstv\'"\\/]|[0-7][0-7]?[0-7]?|\^[a-zA-Z]))}
64
+
65
+ macro_re = %r{(?:#{variable_re}|#{atom_re})}
66
+
67
+ base_re = %r{(?:[2-9]|[12][0-9]|3[0-6])}
68
+
69
+ state :root do
70
+ rule(/\s+/, Text)
71
+ rule(/%.*\n/, Comment)
72
+ rule(%r{(#{keywords.join('|')})\b}, Keyword)
73
+ rule(%r{(#{builtins.join('|')})\b}, Name::Builtin)
74
+ rule(%r{(#{word_operators.join('|')})\b}, Operator::Word)
75
+ rule(/^-/, Punctuation, :directive)
76
+ rule(operators, Operator)
77
+ rule(/"/, Str, :string)
78
+ rule(/<</, Name::Label)
79
+ rule(/>>/, Name::Label)
80
+ rule %r{(#{atom_re})(:)} do
81
+ groups Name::Namespace, Punctuation
82
+ end
83
+ rule %r{(?:^|(?<=:))(#{atom_re})(\s*)(\()} do
84
+ groups Name::Function, Text, Punctuation
85
+ end
86
+ rule(%r{[+-]?#{base_re}#[0-9a-zA-Z]+}, Num::Integer)
87
+ rule(/[+-]?\d+/, Num::Integer)
88
+ rule(/[+-]?\d+.\d+/, Num::Float)
89
+ rule(%r{[\]\[:_@\".{}()|;,]}, Punctuation)
90
+ rule(variable_re, Name::Variable)
91
+ rule(atom_re, Name)
92
+ rule(%r{\?#{macro_re}}, Name::Constant)
93
+ rule(%r{\$(?:#{escape_re}|\\[ %]|[^\\])}, Str::Char)
94
+ rule(%r{##{atom_re}(:?\.#{atom_re})?}, Name::Label)
95
+ end
96
+
97
+ state :string do
98
+ rule(escape_re, Str::Escape)
99
+ rule(/"/, Str, :pop!)
100
+ rule(%r{~[0-9.*]*[~#+bBcdefginpPswWxX]}, Str::Interpol)
101
+ rule(%r{[^"\\~]+}, Str)
102
+ rule(/~/, Str)
103
+ end
104
+
105
+ state :directive do
106
+ rule %r{(define)(\s*)(\()(#{macro_re})} do
107
+ groups Name::Entity, Text, Punctuation, Name::Constant
108
+ pop!
109
+ end
110
+ rule %r{(record)(\s*)(\()(#{macro_re})} do
111
+ groups Name::Entity, Text, Punctuation, Name::Label
112
+ pop!
113
+ end
114
+ rule(atom_re, Name::Entity, :pop!)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,302 @@
1
+ # -*- coding: utf-8 -*- #
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class Factor < RegexLexer
6
+ title "Factor"
7
+ desc "Factor, the practical stack language (factorcode.org)"
8
+ tag 'factor'
9
+ filenames '*.factor'
10
+ mimetypes 'text/x-factor'
11
+
12
+ def self.analyze_text(text)
13
+ return 1 if text.shebang? 'factor'
14
+ end
15
+
16
+ def self.builtins
17
+ @builtins ||= {}.tap do |builtins|
18
+ builtins[:kernel] = Set.new %w(
19
+ or 2bi 2tri while wrapper nip 4dip wrapper? bi*
20
+ callstack>array both? hashcode die dupd callstack
21
+ callstack? 3dup tri@ pick curry build ?execute 3bi prepose
22
+ >boolean if clone eq? tri* ? = swapd 2over 2keep 3keep clear
23
+ 2dup when not tuple? dup 2bi* 2tri* call tri-curry object bi@
24
+ do unless* if* loop bi-curry* drop when* assert= retainstack
25
+ assert? -rot execute 2bi@ 2tri@ boa with either? 3drop bi
26
+ curry? datastack until 3dip over 3curry tri-curry* tri-curry@
27
+ swap and 2nip throw bi-curry (clone) hashcode* compose 2dip if
28
+ 3tri unless compose? tuple keep 2curry equal? assert tri 2drop
29
+ most <wrapper> boolean? identity-hashcode identity-tuple?
30
+ null new dip bi-curry@ rot xor identity-tuple boolean
31
+ )
32
+
33
+ builtins[:assocs] = Set.new %w(
34
+ ?at assoc? assoc-clone-like assoc= delete-at* assoc-partition
35
+ extract-keys new-assoc value? assoc-size map>assoc push-at
36
+ assoc-like key? assoc-intersect assoc-refine update
37
+ assoc-union assoc-combine at* assoc-empty? at+ set-at
38
+ assoc-all? assoc-subset? assoc-hashcode change-at assoc-each
39
+ assoc-diff zip values value-at rename-at inc-at enum? at cache
40
+ assoc>map <enum> assoc assoc-map enum value-at* assoc-map-as
41
+ >alist assoc-filter-as clear-assoc assoc-stack maybe-set-at
42
+ substitute assoc-filter 2cache delete-at assoc-find keys
43
+ assoc-any? unzip
44
+ )
45
+
46
+ builtins[:combinators] = Set.new %w(
47
+ case execute-effect no-cond no-case? 3cleave>quot 2cleave
48
+ cond>quot wrong-values? no-cond? cleave>quot no-case case>quot
49
+ 3cleave wrong-values to-fixed-point alist>quot case-find
50
+ cond cleave call-effect 2cleave>quot recursive-hashcode
51
+ linear-case-quot spread spread>quot
52
+ )
53
+
54
+ builtins[:math] = Set.new %w(
55
+ number= if-zero next-power-of-2 each-integer ?1+
56
+ fp-special? imaginary-part unless-zero float>bits number?
57
+ fp-infinity? bignum? fp-snan? denominator fp-bitwise= *
58
+ + power-of-2? - u>= / >= bitand log2-expects-positive <
59
+ log2 > integer? number bits>double 2/ zero? (find-integer)
60
+ bits>float float? shift ratio? even? ratio fp-sign bitnot
61
+ >fixnum complex? /i /f byte-array>bignum when-zero sgn >bignum
62
+ next-float u< u> mod recip rational find-last-integer >float
63
+ (all-integers?) 2^ times integer fixnum? neg fixnum sq bignum
64
+ (each-integer) bit? fp-qnan? find-integer complex <fp-nan>
65
+ real double>bits bitor rem fp-nan-payload all-integers?
66
+ real-part log2-expects-positive? prev-float align unordered?
67
+ float fp-nan? abs bitxor u<= odd? <= /mod rational? >integer
68
+ real? numerator
69
+ )
70
+
71
+ builtins[:sequences] = Set.new %w(
72
+ member-eq? append assert-sequence= find-last-from
73
+ trim-head-slice clone-like 3sequence assert-sequence? map-as
74
+ last-index-from reversed index-from cut* pad-tail
75
+ remove-eq! concat-as but-last snip trim-tail nths
76
+ nth 2selector sequence slice? <slice> partition
77
+ remove-nth tail-slice empty? tail* if-empty
78
+ find-from virtual-sequence? member? set-length
79
+ drop-prefix unclip unclip-last-slice iota map-sum
80
+ bounds-error? sequence-hashcode-step selector-for
81
+ accumulate-as map start midpoint@ (accumulate) rest-slice
82
+ prepend fourth sift accumulate! new-sequence follow map! like
83
+ first4 1sequence reverse slice unless-empty padding virtual@
84
+ repetition? set-last index 4sequence max-length set-second
85
+ immutable-sequence first2 first3 replicate-as reduce-index
86
+ unclip-slice supremum suffix! insert-nth trim-tail-slice
87
+ tail 3append short count suffix concat flip filter sum
88
+ immutable? reverse! 2sequence map-integers delete-all start*
89
+ indices snip-slice check-slice sequence? head map-find
90
+ filter! append-as reduce sequence= halves collapse-slice
91
+ interleave 2map filter-as binary-reduce slice-error? product
92
+ bounds-check? bounds-check harvest immutable virtual-exemplar
93
+ find produce remove pad-head last replicate set-fourth
94
+ remove-eq shorten reversed? map-find-last 3map-as
95
+ 2unclip-slice shorter? 3map find-last head-slice pop* 2map-as
96
+ tail-slice* but-last-slice 2map-reduce iota? collector-for
97
+ accumulate each selector append! new-resizable cut-slice
98
+ each-index head-slice* 2reverse-each sequence-hashcode
99
+ pop set-nth ?nth <flat-slice> second join when-empty
100
+ collector immutable-sequence? <reversed> all? 3append-as
101
+ virtual-sequence subseq? remove-nth! push-either new-like
102
+ length last-index push-if 2all? lengthen assert-sequence
103
+ copy map-reduce move third first 3each tail? set-first prefix
104
+ bounds-error any? <repetition> trim-slice exchange surround
105
+ 2reduce cut change-nth min-length set-third produce-as
106
+ push-all head? delete-slice rest sum-lengths 2each head*
107
+ infimum remove! glue slice-error subseq trim replace-slice
108
+ push repetition map-index trim-head unclip-last mismatch
109
+ )
110
+
111
+ builtins[:namespaces] = Set.new %w(
112
+ global +@ change set-namestack change-global init-namespaces
113
+ on off set-global namespace set with-scope bind with-variable
114
+ inc dec counter initialize namestack get get-global make-assoc
115
+ )
116
+
117
+ builtins[:arrays] = Set.new %w(
118
+ <array> 2array 3array pair >array 1array 4array pair?
119
+ array resize-array array?
120
+ )
121
+
122
+ builtins[:io] = Set.new %w(
123
+ +character+ bad-seek-type? readln each-morsel
124
+ stream-seek read print with-output-stream contents
125
+ write1 stream-write1 stream-copy stream-element-type
126
+ with-input-stream stream-print stream-read stream-contents
127
+ stream-tell tell-output bl seek-output bad-seek-type nl
128
+ stream-nl write flush stream-lines +byte+ stream-flush
129
+ read1 seek-absolute? stream-read1 lines stream-readln
130
+ stream-read-until each-line seek-end with-output-stream*
131
+ seek-absolute with-streams seek-input seek-relative?
132
+ input-stream stream-write read-partial seek-end?
133
+ seek-relative error-stream read-until with-input-stream*
134
+ with-streams* tell-input each-block output-stream
135
+ stream-read-partial each-stream-block each-stream-line
136
+ )
137
+
138
+ builtins[:strings] = Set.new %w(
139
+ resize-string >string <string> 1string string string?
140
+ )
141
+
142
+ builtins[:vectors] = Set.new %w(
143
+ with-return restarts return-continuation with-datastack
144
+ recover rethrow-restarts <restart> ifcc set-catchstack
145
+ >continuation< cleanup ignore-errors restart?
146
+ compute-restarts attempt-all-error error-thread
147
+ continue <continuation> attempt-all-error? condition?
148
+ <condition> throw-restarts error catchstack continue-with
149
+ thread-error-hook continuation rethrow callcc1
150
+ error-continuation callcc0 attempt-all condition
151
+ continuation? restart return
152
+ )
153
+
154
+ builtins[:continuations] = Set.new %w(
155
+ with-return restarts return-continuation with-datastack
156
+ recover rethrow-restarts <restart> ifcc set-catchstack
157
+ >continuation< cleanup ignore-errors restart?
158
+ compute-restarts attempt-all-error error-thread
159
+ continue <continuation> attempt-all-error? condition?
160
+ <condition> throw-restarts error catchstack continue-with
161
+ thread-error-hook continuation rethrow callcc1
162
+ error-continuation callcc0 attempt-all condition
163
+ continuation? restart return
164
+ )
165
+ end
166
+ end
167
+
168
+ state :root do
169
+ rule /\s+/m, Text
170
+
171
+ rule /(:|::|MACRO:|MEMO:|GENERIC:|HELP:)(\s+)(\S+)/m do
172
+ groups Keyword, Text, Name::Function
173
+ end
174
+
175
+ rule /(M:|HOOK:|GENERIC#)(\s+)(\S+)(\s+)(\S+)/m do
176
+ groups Keyword, Text, Name::Class, Text, Name::Function
177
+ end
178
+
179
+ rule /\((?=\s)/, Name::Function, :stack_effect
180
+ rule /;(?=\s)/, Keyword
181
+
182
+ rule /(USING:)((?:\s|\\\s)+)/m do
183
+ groups Keyword::Namespace, Text
184
+ push :import
185
+ end
186
+
187
+ rule /(IN:|USE:|UNUSE:|QUALIFIED:|QUALIFIED-WITH:)(\s+)(\S+)/m do
188
+ groups Keyword::Namespace, Text, Name::Namespace
189
+ end
190
+
191
+ rule /(FROM:|EXCLUDE:)(\s+)(\S+)(\s+)(=>)/m do
192
+ groups Keyword::Namespace, Text, Name::Namespace, Text, Punctuation
193
+ end
194
+
195
+ rule /(?:ALIAS|DEFER|FORGET|POSTPONE):/, Keyword::Namespace
196
+
197
+ rule /(TUPLE:)(\s+)(\S+)(\s+)(<)(\s+)(\S+)/m do
198
+ groups(
199
+ Keyword, Text,
200
+ Name::Class, Text,
201
+ Punctuation, Text,
202
+ Name::Class
203
+ )
204
+ push :slots
205
+ end
206
+
207
+ rule /(TUPLE:)(\s+)(\S+)/m do
208
+ groups Keyword, Text, Name::Class
209
+ push :slots
210
+ end
211
+
212
+ rule /(UNION:|INTERSECTION:)(\s+)(\S+)/m do
213
+ groups Keyword, Text, Name::Class
214
+ end
215
+
216
+ rule /(PREDICATE:)(\s+)(\S+)(\s+)(<)(\s+)(\S+)/m do
217
+ groups(
218
+ Keyword, Text,
219
+ Name::Class, Text,
220
+ Punctuation, Text,
221
+ Name::Class
222
+ )
223
+ end
224
+
225
+ rule /(C:)(\s+)(\S+)(\s+)(\S+)/m do
226
+ groups(
227
+ Keyword, Text,
228
+ Name::Function, Text,
229
+ Name::Class
230
+ )
231
+ end
232
+
233
+ rule %r(
234
+ (INSTANCE|SLOT|MIXIN|SINGLETONS?|CONSTANT|SYMBOLS?|ERROR|SYNTAX
235
+ |ALIEN|TYPEDEF|FUNCTION|STRUCT):
236
+ )x, Keyword
237
+
238
+ rule /(?:<PRIVATE|PRIVATE>)/, Keyword::Namespace
239
+
240
+ rule /(MAIN:)(\s+)(\S+)/ do
241
+ groups Keyword::Namespace, Text, Name::Function
242
+ end
243
+
244
+ # strings
245
+ rule /"""\s+.*?\s+"""/, Str
246
+ rule /"(\\.|[^\\])*?"/, Str
247
+ rule /(CHAR:)(\s+)(\\[\\abfnrstv]*|\S)(?=\s)/, Str::Char
248
+
249
+ # comments
250
+ rule /!\s+.*$/, Comment
251
+ rule /#!\s+.*$/, Comment
252
+
253
+ # booleans
254
+ rule /[tf](?=\s)/, Name::Constant
255
+
256
+ # numbers
257
+ rule /-?\d+\.\d+(?=\s)/, Num::Float
258
+ rule /-?\d+(?=\s)/, Num::Integer
259
+
260
+ rule /HEX:\s+[a-fA-F\d]+(?=\s)/m, Num::Hex
261
+ rule /BIN:\s+[01]+(?=\s)/, Num::Bin
262
+ rule /OCT:\s+[0-7]+(?=\s)/, Num::Oct
263
+
264
+ rule %r([-+/*=<>^](?=\s)), Operator
265
+
266
+ rule /(?:deprecated|final|foldable|flushable|inline|recursive)(?=\s)/,
267
+ Keyword
268
+
269
+ rule /\S+/ do |m|
270
+ name = m[0]
271
+
272
+ if self.class.builtins.values.any? { |b| b.include? name }
273
+ token Name::Builtin
274
+ else
275
+ token Name
276
+ end
277
+ end
278
+ end
279
+
280
+ state :stack_effect do
281
+ rule /\s+/, Text
282
+ rule /\(/, Name::Function, :stack_effect
283
+ rule /\)/, Name::Function, :pop!
284
+
285
+ rule /--/, Name::Function
286
+ rule /\S+/, Name::Variable
287
+ end
288
+
289
+ state :slots do
290
+ rule /\s+/, Text
291
+ rule /;(?=\s)/, Keyword, :pop!
292
+ rule /\S+/, Name::Variable
293
+ end
294
+
295
+ state :import do
296
+ rule /;(?=\s)/, Keyword, :pop!
297
+ rule /\s+/, Text
298
+ rule /\S+/, Name::Namespace
299
+ end
300
+ end
301
+ end
302
+ end