Added D parser.
[ohcount] / ext / ohcount_native / generator.rb
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                         actionscript = CMonoglot.new("actionscript",   '//',             [e('/*'), e('*/')], true,  true)
24                         ada = CMonoglot.new("ada",                 '--',             nil,                true,  false)
25                         assembler = CMonoglot.new("assembler",     [';', '!', '//'], [e('/*'), e('*/')], false, false)
26                         autoconf = LineCommentMonoglot.new("autoconf", 'dnl')
27                         automake = LineCommentMonoglot.new("automake", '#')
28                         awk = CMonoglot.new("awk",                 '#',              nil,                true,  false, {:no_escape_dquote => true})
29                         bat = LineCommentMonoglot.new("bat",        '^\\\\s*(?i)REM(?-i)')
30                         boo = PythonMonoglot.new("boo")
31                         clearsilver = CMonoglot.new("clearsilver", '#',              nil,                true,  true)
32                         c = CMonoglot.new("c",                     '//',             [e('/*'), e('*/')], true,  true)
33                         cpp = CMonoglot.new("cpp",                 '//',             [e('/*'), e('*/')], true,  true)
34                         csharp = CMonoglot.new("csharp",           '//',             [e('/*'), e('*/')], true,  false)
35                         css = CMonoglot.new("css",                  nil,             [e('/*'), e('*/')], false,  false)
36                         dcl = DclMonoglot.new("dcl")
37                         dylan = CMonoglot.new("dylan",             '//',             nil,                true,  false)
38                         ebuild = CMonoglot.new("ebuild",           '#',              nil,                false, false)
39                         erlang = CMonoglot.new("erlang",           '%%',             nil,                true,  true)
40                         java = CMonoglot.new("java",               '//',             [e('/*'), e('*/')], true,  false)
41                         javascript = CMonoglot.new("javascript",   '//',             [e('/*'), e('*/')], true,  true)
42                         emacslisp = LineCommentMonoglot.new("emacslisp", ";")
43                         fortranfixed = CMonoglot.new("fortranfixed", '^[^ \n]',          nil,                true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
44                         fortranfree  = CMonoglot.new("fortranfree",  '!',            nil,                true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
45                         haskell = CMonoglot.new("haskell",         '--',             [e('{-'), e('-}')], true, false)
46                         lisp = LineCommentMonoglot.new("lisp", ";")
47                         lua = CMonoglot.new("lua",                 '--',             nil,                true,  true)
48                         make = LineCommentMonoglot.new("make",     '#')
49                         matlab = CMonoglot.new("matlab",           '#|%',            ['{%', '%}'], false,true)
50                         metafont = LineCommentMonoglot.new("metafont", "%");
51                         metapost = LineCommentMonoglot.new("metapost", "%");
52                         objective_c = CMonoglot.new("objective_c", '//',             [e('/*'), e('*/')], true,  false)
53                         pascal = CMonoglot.new("pascal",           '//',             ['{','}'],          true,  true, {:no_escape_dquote => true, :no_escape_squote => true})
54                         perl = CMonoglot.new("perl",               '#',              ['^=\\\\w+', '^=cut[ \t]*\\\\n'],  true,  true)
55                         phplanguage = CMonoglot.new("php",         ['//', '#'],             [e('/*'), e('*/')], true,  true, {:polyglot_name => 'phplanguage'})
56                         pike = CMonoglot.new("pike",             '//',             [e('/*'), e('*/')], true,  false)
57                         python = PythonMonoglot.new("python")
58                         ruby = CMonoglot.new("ruby",               '#',              nil,                true,  true)
59                         rexx = CMonoglot.new("rexx",               nil,              [e('/*'), e('*/')], true,  true)
60                         scheme = LineCommentMonoglot.new("scheme", ";")
61                         shell = CMonoglot.new("shell",             '#',              nil,                false, false)
62                         smalltalk = CMonoglot.new("smalltalk",            nil,             [e('"'), e('"')], false,  true, options = {:no_escape_squote => true})
63                         sql = CMonoglot.new("sql",                 ['--','//', '#'],      [['{','}'], [e('/*'), e('*/')]], true, true)
64                         tcl = CMonoglot.new("tcl",                 '#',              nil,                true,  false)
65                         vala = CMonoglot.new("vala",               '//',             [e('/*'), e('*/')], true,  false)
66                         vim = CMonoglot.new("vim",                 '\"',             nil,                false, false)
67                         visualbasic = CMonoglot.new("visualbasic", '\'',             nil,                true,  false)
68                         xml = XmlMonoglot.new("xml")
69                         xslt = XmlMonoglot.new("xslt")
70                         xmlschema = XmlMonoglot.new("xmlschema")
71                         mxml = MxmlPolyglot.new("mxml", actionscript, css)
72                         html = HtmlPolyglot.new("html", javascript, css)
73                         php = HtmlWithPhpPolyglot.new("php", html, phplanguage)
74                         rhtml = RhtmlPolyglot.new("rhtml", html, ruby)
75                         jsp = JspPolyglot.new("jsp", html, java)
76                         groovy = CMonoglot.new("groovy",           '//',             [e('/*'), e('*/')], true,  false)
77                         clearsilver_template = ClearsilverTemplate.new("clearsilver_template", html, clearsilver)
78                         dmd = DMonoglot.new('dmd')
79                         tex = CMonoglot.new("tex",             '%',              nil,                false, false)
80                         metapost_with_tex = Biglot.new('metapost_with_tex', metapost, tex, [], [
81                                 ["verbatimtex", :metapost_code, :tex_code, :from, false, 'verbatim'],
82                                 ["btex", :metapost_code, :tex_code, :from, false, 'btex'],
83                                 ["etex", :tex_code, :return, :to, false, 'etex']
84                         ])
85                         scala = CMonoglot.new("scala",               '//',             [e('/*'), e('*/')], true,  false)
86
87                         polyglots = [
88                           actionscript ,
89                                 ada ,
90                                 assembler ,
91                                 autoconf ,
92                                 automake ,
93                                 awk ,
94                                 bat ,
95                                 boo ,
96                                 clearsilver ,
97                                 c ,
98                                 cpp ,
99                                 csharp ,
100                                 css ,
101         dcl,
102                                 dylan ,
103                                 ebuild ,
104                                 erlang ,
105                                 groovy ,
106                                 java ,
107                                 javascript ,
108                                 emacslisp ,
109                                 fortranfixed ,
110                                 fortranfree ,
111                                 haskell,
112                                 lisp ,
113                                 lua ,
114                                 make ,
115                                 matlab,
116                                 metafont,
117                                 metapost,
118                                 objective_c,
119                                 pascal ,
120                                 perl ,
121                                 pike ,
122                                 phplanguage ,
123                                 python ,
124                                 ruby ,
125                                 rexx ,
126                                 scheme ,
127                                 scala,
128                                 shell ,
129                                 smalltalk ,
130                                 sql ,
131                                 tcl ,
132                                 vala ,
133                                 visualbasic ,
134                                 vim ,
135                                 xml ,
136                                 xmlschema ,
137                                 xslt ,
138                                 dmd ,
139
140                                 # poly
141                                 html,
142                                 php,
143                                 rhtml,
144                                 jsp,
145                                 clearsilver_template,
146                                 tex,
147                                 metapost_with_tex,
148                                 mxml
149                         ]
150                         File.open("polyglots.c", "w") do |io|
151
152                                 # spit out the preamble to our source code
153                                 io.puts <<PREAMBLE
154 /*
155  * polyglots.c
156  * Ohcount
157  *
158  * GENERATED FILE **DO NOT EDIT**
159  *
160  */
161
162 #define __polyglots_c__
163 #include "common.h"
164
165 #define RETURN (State *)NULL
166 PREAMBLE
167
168                                 # spits out the actual POLYGLOTS array, which contains a reference to all the polyglots we define in our library
169                                 polyglots.each do |p|
170                                         p.print(io)
171                                 end
172                                 io.puts "\n"
173                                 Monoglot::print_banner(io, "POLYGLOTS")
174                                 io.puts "Polyglot *POLYGLOTS[] = {"
175                                 polyglots.each do |p|
176                                         io.puts "       &#{ p.definition },"
177                                 end
178                                 io.puts "       NULL\n};"
179                         end
180                 end
181         end
182 end
183
184 Ohcount::Generator.new.generate