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