-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest.rb
More file actions
50 lines (42 loc) · 1.25 KB
/
test.rb
File metadata and controls
50 lines (42 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
__END__
#
# later on the idea is to add a Ometa::Grammar function or something similar to wrap
# up the process of creating a ruby class object from an ometa grammar file.
#
# as an example (this is missing the optimization pass):
#
class Ometa
def self.Grammar(filename)
grammar = File.read(filename)
ast = OMetaParser.parsewith(grammar, 'grammar')
ruby = RubyOMetaTranslator.matchwith(ast, 'trans')
#ruby = File.read('_debug.rb')
begin
open('_debug.rb', 'w') { |f| f << ruby }
eval ruby
rescue SyntaxError
puts '* error compiling grammar'
puts '* ast:'
require 'pp'
pp ast
puts '* ruby:'
puts ruby
open('_debug.rb', 'w') { |f| f << ruby }
raise
end
end
end
class Calc < Ometa::Grammar('calc.ometa')
def self.calc str
matchAllwith str, 'expr'
end
end
class JSParser < Ometa::Grammar('js_parser.ometa')
KEYWORDS = ["break", "case", "catch", "continue", "default", "delete", "do", "else", "finally", "for", "function", "if", "in",
"instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", "ometa"]
KEYWORDS_HASH = KEYWORDS.inject({}) { |h, k| h.update k => true }
def self._isKeyword k
KEYWORDS_HASH[k.to_s]
end
end
p JSParser.parsewith(" x += 1;\n", 'expr')