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