Support for VIMscript
[ohcount] / ext / ohcount_native / generator.rb.orig
1 require 'state'
2 require 'transition'
3 require 'escape_helper'
4
5 # So that monoglot and polyglot source files can easily require eachother
6 $LOAD_PATH << File.join(File.dirname(__FILE__), 'glots')
7
8 # Load all monoglots and polyglots
9 Dir.glob(File.join(File.dirname(__FILE__), 'glots/*.rb')).each {|f| require f }
10
11 module Ohcount
12         class Generator
13                 include EscapeHelper
14
15                 # This script loads all of the Monoglot and Polyglot files found in
16                 # <tt>ext/ohcount_native/glots</tt>.
17                 #
18                 # These glots are used to generate the C file polyglots.c, which will define
19                 # all of the languages parsers used by ohcount. Do not edit polyglots.c directly.
20                 def generate
21
22                         # Defines all of the monoglots and polyglots known to the parser.
23                         ada = CMonoglot.new("ada",                 '--',             nil,                true,  false)
24                         assembler = CMonoglot.new("assembler",     [';', '!', '//'], [e('/*'), e('*/')], false, false)
25                         awk = CMonoglot.new("awk",                 '#',              nil,                true,  false, {:no_escape_dquote => true})
26                         bat = LineCommentMonoglot.new("bat",        '^\\\\s*(?i)REM(?-i)')
27                         boo = PythonMonoglot.new("boo")
28                         clearsilver = CMonoglot.new("clearsilver", '#',              nil,                true,  true)
29                         c = CMonoglot.new("c",                     '//',             [e('/*'), e('*/')], true,  true)
30                         cpp = CMonoglot.new("cpp",                 '//',             [e('/*'), e('*/')], true,  true)
31                         csharp = CMonoglot.new("csharp",           '//',             [e('/*'), e('*/')], true,  false)
32                         css = CMonoglot.new("css",                  nil,             [e('/*'), e('*/')], false,  false)
33                         dcl = DclMonoglot.new("dcl")
34                         dylan = CMonoglot.new("dylan",             '//',             nil,                true,  false)
35                         erlang = CMonoglot.new("erlang",           '%%',             nil,                true,  true)
36                         java = CMonoglot.new("java",               '//',             [e('/*'), e('*/')], true,  false)
37                         javascript = CMonoglot.new("javascript",   '//',             [e('/*'), e('*/')], true,  true)
38                         emacslisp = LineCommentMonoglot.new("emacslisp", ";")
39                         fortranfixed = CMonoglot.new("fortranfixed", '^[^ \n]',          nil,                true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
40                         fortranfree  = CMonoglot.new("fortranfree",  '!',            nil,                true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
41                         haskell = CMonoglot.new("haskell",         '--',             [e('{-'), e('-}')], true, false)
42                         lisp = LineCommentMonoglot.new("lisp", ";")
43                         lua = CMonoglot.new("lua",                 '--',             nil,                true,  true)
44                         matlab = CMonoglot.new("matlab",           '#|%',            ['{%', '%}'], false,true)
45                         metafont = LineCommentMonoglot.new("metafont", "%");
46                         metapost = LineCommentMonoglot.new("metapost", "%");
47                         objective_c = CMonoglot.new("objective_c", '//',             [e('/*'), e('*/')], true,  false)
48                         pascal = CMonoglot.new("pascal",           '//',             ['{','}'],          true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
49                         perl = CMonoglot.new("perl",               '#',              ['^=\\\\w+', '^=cut[ \t]*\\\\n'],  true,  true)
50                         phplanguage = CMonoglot.new("php",         '//',             [e('/*'), e('*/')], true,  true, {:polyglot_name => 'phplanguage'})
51                         pike = CMonoglot.new("pike",             '//',             [e('/*'), e('*/')], true,  false)
52                         python = PythonMonoglot.new("python")
53                         ruby = CMonoglot.new("ruby",               '#',              nil,                true,  true)
54                         rexx = CMonoglot.new("rexx",               nil,              [e('/*'), e('*/')], true,  true)
55                         scheme = LineCommentMonoglot.new("scheme", ";")
56                         shell = CMonoglot.new("shell",             '#',              nil,                false, false)
57                         smalltalk = CMonoglot.new("smalltalk",            nil,             [e('"'), e('"')], false,  true, options = {:no_escape_squote => true})
58                         sql = CMonoglot.new("sql",                 ['--','//'],      [['{','}'], [e('/*'), e('*/')]], true, true)
59                         tcl = CMonoglot.new("tcl",                 '#',              nil,                true,  false)
60                         vala = CMonoglot.new("vala",               '//',             [e('/*'), e('*/')], true,  false)
61                         visualbasic = CMonoglot.new("visualbasic", '\'',             nil,                true,  false)
62                         xml = XmlMonoglot.new("xml")
63                         xslt = XmlMonoglot.new("xslt")
64                         xmlschema = XmlMonoglot.new("xmlschema")
65                         html = HtmlPolyglot.new("html", javascript, css)
66                         php = HtmlWithPhpPolyglot.new("php", html, phplanguage)
67                         rhtml = RhtmlPolyglot.new("rhtml", html, ruby)
68                         jsp = JspPolyglot.new("jsp", html, java)
69                         groovy = CMonoglot.new("groovy",           '//',             [e('/*'), e('*/')], true,  false)
70                         clearsilver_template = ClearsilverTemplate.new("clearsilver_template", html, clearsilver)
71                         dmd = DMonoglot.new('dmd')
72                         tex = CMonoglot.new("tex",             '%',              nil,                false, false)
73                         metapost_with_tex = Biglot.new('metapost_with_tex', metapost, tex, [], [
74                                 ["verbatimtex", :metapost_code, :tex_code, :from, false, 'verbatim'],
75                                 ["btex", :metapost_code, :tex_code, :from, false, 'btex'],
76                                 ["etex", :tex_code, :return, :to, false, 'etex']
77                         ]);
78                         polyglots = [
79                                 ada ,
80                                 assembler ,
81                                 awk ,
82                                 bat ,
83                                 boo ,
84                                 clearsilver ,
85                                 c ,
86                                 cpp ,
87                                 csharp ,
88                                 css ,
89                                 dcl,
90                                 dylan ,
91                                 erlang ,
92                                 groovy ,
93                                 java ,
94                                 javascript ,
95                                 emacslisp ,
96                                 fortranfixed ,
97                                 fortranfree ,
98                                 haskell,
99                                 lisp ,
100                                 lua ,
101                                 matlab,
102                                 metafont,
103                                 metapost,
104                                 objective_c,
105                                 pascal ,
106                                 perl ,
107                                 pike ,
108                                 phplanguage ,
109                                 python ,
110                                 ruby ,
111                                 rexx ,
112                                 scheme ,
113                                 shell ,
114                                 smalltalk ,
115                                 sql ,
116                                 tcl ,
117                                 vala ,
118                                 visualbasic ,
119                                 xml ,
120                                 xmlschema ,
121                                 xslt ,
122                                 dmd ,
123
124                                 # poly
125                                 html,
126                                 php,
127                                 rhtml,
128                                 jsp,
129                                 clearsilver_template,
130                                 tex,
131                                 metapost_with_tex
132                         ]
133                         File.open("polyglots.c", "w") do |io|
134
135                                 # spit out the preamble to our source code
136                                 io.puts <<PREAMBLE
137 /*
138  * polyglots.c
139  * Ohcount
140  *
141  * GENERATED FILE **DO NOT EDIT**
142  *
143  */
144
145 #define __polyglots_c__
146 #include "common.h"
147
148 #define RETURN (State *)NULL
149 PREAMBLE
150
151                                 # spits out the actual POLYGLOTS array, which contains a reference to all the polyglots we define in our library
152                                 polyglots.each do |p|
153                                         p.print(io)
154                                 end
155                                 io.puts "\n"
156                                 Monoglot::print_banner(io, "POLYGLOTS")
157                                 io.puts "Polyglot *POLYGLOTS[] = {"
158                                 polyglots.each do |p|
159                                         io.puts "       &#{ p.definition },"
160                                 end
161                                 io.puts "       NULL\n};"
162                         end
163                 end
164         end
165 end
166
167 Ohcount::Generator.new.generate