Permalink
Browse files

Finalise switch over to braced semantic actions

Switch over and clean up the ometa core grammars to use the braced semantic actions.
  • Loading branch information...
1 parent 64c036b commit a3b2f254fa2d47c9328e428e0cb7d00540434caf Charles Lowe committed Sep 18, 2008
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -473,12 +473,12 @@ def self.parsewith(text, rule)
raise e, message #, $@
end
end
-end
-# this doesn't really belong here. mostly a relic of the javascript port
-class String
- def toProgramString
- inspect
+ ESCAPE_LOOKUP = {'n' => "\n", 't' => "\t", 'r' => "\r", '\'' => "'", '\"' => '"', '\\' => '\\'}
+
+ def unescapeChar c
+ ESCAPE_LOOKUP[c] or raise NotImplementedError
end
end
+class String; def toProgramString; inspect; end; end
View
@@ -1,16 +1,10 @@
ometa AndOrOptimizer <: NullOptimizer {
- And trans:x end setHelped -> x
- ,
- And transInside('"And"'):xs -> ['And'].concat(xs)
- ,
- Or trans:x end setHelped -> x
- ,
- Or transInside('"Or"'):xs -> ['Or'].concat(xs)
- ,
-
- transInside :t = [exactly('t') transInside('t'):xs] transInside('t'):ys setHelped -> xs.concat(ys)
- | trans:x transInside('t'):xs -> [x].concat(xs)
- | -> []
+ And trans:x end setHelped -> { x },
+ And transInside('"And"'):xs -> { ['And', *xs] },
+ Or trans:x end setHelped -> { x },
+ Or transInside('"Or"'):xs -> { ['Or', *xs] },
+ transInside :t = [exactly('t') transInside('t'):xs] transInside('t'):ys setHelped -> { xs + ys }
+ | trans:x transInside('t'):xs -> { [x, *xs] }
+ | -> { [] }
}
-
View
@@ -1,24 +1,15 @@
ometa BSRubyParser <: OMeta {
- eChar = '\\' char:c -> Hash['n' => "\n", 't' => "\t", 'r' => "\r", '\'' => "'", '\"' => '"', '\\' => '\\'][c]
+ eChar = '\\' char:c -> { unescapeChar c }
| char,
- tsString = '\'' (~'\'' eChar)*:xs '\'' -> xs.join('')
- ,
- expr = spaces tsString
- ,
- oldStyleSemAction = spaces (~seq('"\\n"') anything)*:xs -> xs.join('')
- ,
- nonBraceChar = ~'{' ~'}' char
- ,
- inside = innerMatchedBraces | nonBraceChar
- ,
- innerMatchedBraces = '{' inside*:xs '}' -> "{#{xs.join('')}}"
- ,
- outerMatchedBraces = '{' inside*:xs '}' -> xs.join('')
- ,
- newStyleSemAction = spaces outerMatchedBraces:x -> p 'using new style sem action' => x; x
- ,
- semAction = newStyleSemAction | oldStyleSemAction
-
+ tsString = '\'' (~'\'' eChar)*:xs '\'' -> { xs.join('') },
+ expr = spaces tsString,
+ semAction1 = spaces (~seq('"\\n"') anything)*:xs -> { xs.join('') },
+ nonBraceChar = ~'{' ~'}' char,
+ inside = innerBraces | nonBraceChar,
+ innerBraces = '{' inside*:xs '}' -> { "{#{xs.join('')}}" },
+ outerBraces = '{' inside*:xs '}' -> { xs.join('') },
+ semAction2 = spaces outerBraces,
+ semAction = semAction2 | semAction1
}
View
@@ -1,34 +1,19 @@
ometa NullOptimizer {
- setHelped = !@_didSomething = true
- ,
- helped = ?@_didSomething
- ,
- trans = [:t ?respond_to?(t)
- apply('t'):ans] -> ans
+ setHelped = !{@_didSomething = true},
+ helped = ?{@_didSomething},
+ trans = [:t ?{respond_to?(t)}
+ apply('t'):ans] -> { ans }
| anything,
- optimize = trans:x helped -> x
- ,
-
- Or trans*:xs -> ['Or'].concat(xs)
- ,
- And trans*:xs -> ['And'].concat(xs)
- ,
- Many trans:x -> ['Many', x]
- ,
- Many1 trans:x -> ['Many1', x]
- ,
- Set :n trans:v -> ['Set', n, v]
- ,
- Not trans:x -> ['Not', x]
- ,
- Lookahead trans:x -> ['Lookahead', x]
- ,
- Form trans:x -> ['Form', x]
- ,
- Rule :name :ls trans:body -> ['Rule', name, ls, body]
- ,
-
- initialize -> @_didSomething = false
+ optimize = trans:x helped -> { x },
+ Or trans*:xs -> { ['Or', *xs] },
+ And trans*:xs -> { ['And', *xs] },
+ Many trans:x -> { ['Many', x] },
+ Many1 trans:x -> { ['Many1', x] },
+ Set :n trans:v -> { ['Set', n, v] },
+ Not trans:x -> { ['Not', x] },
+ Lookahead trans:x -> { ['Lookahead', x] },
+ Form trans:x -> { ['Form', x] },
+ Rule :name :ls trans:body -> { ['Rule', name, ls, body] },
+ initialize -> { @_didSomething = false }
}
-
View
@@ -1,5 +1,5 @@
ometa OMetaOptimizer {
- optimizeGrammar = ['Grammar' :n :sn optimizeRule*:rs] -> ['Grammar', n, sn].concat(rs)
- ,
- optimizeRule = :r (foreign('AndOrOptimizer', '"optimize"', 'r'):r)* -> r
+ optimizeGrammar = ['Grammar' :n :sn optimizeRule*:rs] -> { ['Grammar', n, sn, *rs] },
+ optimizeRule = :r (foreign('AndOrOptimizer', '"optimize"', 'r'):r)* -> { r }
}
+
View
@@ -2,71 +2,50 @@
ometa OMetaParser <: OMeta {
nameFirst = '_' | '$' | letter,
nameRest = nameFirst | digit,
- tsName = firstAndRest('"nameFirst"', '"nameRest"'):xs -> xs.join('')
- ,
- name = spaces tsName,
- eChar = '\\' char:c -> Hash['n' => "\n", 't' => "\t", 'r' => "\r", '\'' => "'", '\"' => '"', '\\' => '\\'][c]
+ tsName = firstAndRest('"nameFirst"', '"nameRest"'):xs -> { xs.join('') },
+ name = spaces tsName,
+ eChar = '\\' char:c -> { unescapeChar c }
| char,
- tsString = '\'' (~'\'' eChar)*:xs '\'' -> xs.join('')
- ,
- characters = '`' '`' (~('\'' '\'') eChar)*:xs '\'' '\'' -> ['App', 'seq', xs.join('').toProgramString()]
- ,
- sCharacters = '"' (~'"' eChar)*:xs '"' -> ['App', 'token', xs.join('').toProgramString()]
- ,
- string = (('#' | '`') tsName | tsString):xs -> ['App', 'exactly', xs.toProgramString()]
- ,
- number = ('-' | empty -> ''
- ):sign digit+:ds -> ['App', 'exactly', sign + ds.join('')]
- ,
-
- keyword :xs = token('xs') ~letterOrDigit -> xs
- ,
- hostExpr = foreign('BSRubyParser', '"expr"'):r foreign('BSRubyTranslator', '"trans"', 'r'),
- atomicHostExpr = foreign('BSRubyParser', '"semAction"'):r foreign('BSRubyTranslator', '"trans"', 'r'),
- args = "(" listOf('"hostExpr"', '","'):xs ")" -> xs
- | empty -> []
- ,
- application = name:rule args:as -> ['App', rule].concat(as)
- ,
- semAction = ("!" | "->") atomicHostExpr:x -> ['Act', x]
- ,
- semPred = "?" atomicHostExpr:x -> ['Pred', x]
- ,
- expr = listOf('"expr4"', '"|"'):xs -> ['Or'].concat(xs)
- ,
- expr4 = expr3*:xs -> ['And'].concat(xs)
- ,
- optIter :x = "*" -> ['Many', x]
- | "+" -> ['Many1', x]
- | empty -> x
- ,
- expr3 = expr2:x optIter('x'):x ( ':' name:n -> (@locals << n; ['Set', n, x])
- | empty -> x
- )
- | ":" name:n -> (@locals << n; ['Set', n, ['App', 'anything']])
- ,
- expr2 = "~" expr2:x -> ['Not', x]
- | "&" expr1:x -> ['Lookahead', x]
+ tsString = '\'' (~'\'' eChar)*:xs '\'' -> { xs.join('') },
+ characters = '`' '`' (~('\'' '\'') eChar)*:xs '\'' '\'' -> { ['App', 'seq', xs.join('').inspect] },
+ sCharacters = '"' (~'"' eChar)*:xs '"' -> { ['App', 'token', xs.join('').inspect] },
+ string = (('#' | '`') tsName | tsString):xs -> { ['App', 'exactly', xs.inspect] },
+ number = ('-' | empty -> { '' }):sign digit+:ds -> { ['App', 'exactly', sign + ds.join('')] },
+ keyword :xs = token('xs') ~letterOrDigit -> { xs },
+ hostExpr = foreign('BSRubyParser', '"expr"'):r foreign('BSRubyTranslator', '"trans"', 'r'),
+ atomicHostExpr = foreign('BSRubyParser', '"semAction"'):r foreign('BSRubyTranslator', '"trans"', 'r'),
+ args = "(" listOf('"hostExpr"', '","'):xs ")" -> { xs }
+ | empty -> { [] },
+ application = name:rule args:as -> { ['App', rule, *as] },
+ semAction = ("!" | "->") atomicHostExpr:x -> { ['Act', x] },
+ semPred = "?" atomicHostExpr:x -> { ['Pred', x] },
+ expr = listOf('"expr4"', '"|"'):xs -> { ['Or', *xs] },
+ expr4 = expr3*:xs -> { ['And', *xs] },
+ optIter :x = "*" -> { ['Many', x] }
+ | "+" -> { ['Many1', x] }
+ | empty -> { x },
+ expr3 = expr2:x optIter('x'):x ( ':' name:n -> { (@locals << n; ['Set', n, x]) }
+ | empty -> x
+ )
+ | ":" name:n -> { (@locals << n; ['Set', n, ['App', 'anything']]) },
+ expr2 = "~" expr2:x -> { ['Not', x] }
+ | "&" expr1:x -> { ['Lookahead', x] }
| expr1,
expr1 = application | semAction | semPred
| ( keyword('"undefined"') | keyword('"nil"')
- | keyword('"true"') | keyword('"false"') ):x -> ['App', 'exactly', x]
+ | keyword('"true"') | keyword('"false"') ):x -> { ['App', 'exactly', x] }
| spaces (characters | sCharacters | string | number)
- | "[" expr:x "]" -> ['Form', x]
- | "(" expr:x ")" -> x
- ,
+ | "[" expr:x "]" -> { ['Form', x] }
+ | "(" expr:x ")" -> { x },
ruleName = name
| spaces tsString,
- rule = &(ruleName:n) !@locals = []
- rulePart('n'):x ("," rulePart('n'))*:xs -> ['Rule', n, @locals, ['Or', x].concat(xs)]
- ,
- rulePart :rn = ruleName:n ?n == rn
- expr4:b1 ( "=" expr:b2 -> ['And', b1, b2]
- | empty -> b1
+ rule = &(ruleName:n) !{@locals = []}
+ rulePart('n'):x ("," rulePart('n'))*:xs -> { ['Rule', n, @locals, ['Or', x, *xs]] },
+ rulePart :rn = ruleName:n ?{n == rn} expr4:b1 ( "=" expr:b2 -> { ['And', b1, b2] }
+ | empty -> { b1 }
),
grammar = keyword('"ometa"') name:n
- ( "<:" name | empty -> OMeta
- ):sn
- "{" listOf('"rule"', '","'):rs "}" -> ['Grammar', n, sn].concat(rs)
+ ( "<:" name | empty -> { 'OMeta' }):sn
+ "{" listOf('"rule"', '","'):rs "}" -> { ['Grammar', n, sn, *rs] }
}
@@ -1,43 +1,25 @@
ometa RubyOMetaTranslator {
- trans [:t apply('t'):ans] -> ans
- ,
- App 'super' anything+:args -> ['_superApplyWithArgs(', args.join(','), ')'].join('')
- ,
- App :rule anything+:args -> ['_applyWithArgs("', rule, '",', args.join(','), ')'].join('')
- ,
- App :rule -> ['_apply("', rule, '")'] .join('')
- ,
- Act :expr -> expr
- ,
- Pred :expr -> "_pred(#{expr})"
- ,
- Or transFn*:xs -> "_or(#{xs * ', '})"
- ,
- And notLast('"trans"')*:xs trans:y -> "(#{(xs + [y]) * ';'})"
- ,
- And -> "proc {}"
- ,
- Many trans:x -> "_xmany { #{x} }"
- ,
- Many1 trans:x -> "_xmany1 { #{x} }"
- ,
- Set :n trans:v -> "#{n} = #{v}"
- ,
- Not trans:x -> "_xnot { #{x} }"
- ,
- Lookahead trans:x -> "_xlookahead { #{x} }"
- ,
- Form trans:x -> "_xform { #{x} }"
- ,
- Rule :name locals:ls trans:body -> "def #{name}\n#{ls}\n#{body}\nend\n"
- ,
+ trans [:t apply('t'):ans] -> { ans },
+ App 'super' anything+:args -> { "_superApplyWithArgs(#{args * ', '})" },
+ App :rule anything+:args -> { "_applyWithArgs(#{rule.inspect}, #{args * ', '})" },
+ App :rule -> { "_apply(#{rule.inspect})" },
+ Act :expr -> { expr },
+ Pred :expr -> { "_pred(#{expr})" },
+ Or transFn*:xs -> { "_or(#{xs * ', '})" },
+ And notLast('"trans"')*:xs trans:y -> { "(#{(xs + [y]) * ';'})" },
+ And -> { "proc {}" },
+ Many trans:x -> { "_xmany { #{x} }" },
+ Many1 trans:x -> { "_xmany1 { #{x} }" },
+ Set :n trans:v -> { "#{n} = #{v}" },
+ Not trans:x -> { "_xnot { #{x} }" },
+ Lookahead trans:x -> { "_xlookahead { #{x} }" },
+ Form trans:x -> { "_xform { #{x} }" },
+ Rule :name locals:ls trans:body -> { "def #{name}\n#{ls}\n#{body}\nend\n" },
Grammar :name :sName
- trans*:rules -> ["Class.new(", sName, ") do\n@name = ", name.inspect, "\n", rules.join("\n"), 'end'] .join('')
- ,
- locals = [anything+:vs] -> vs.map { |v| "#{v} = " }.join + 'nil'
- | [] -> ''
- ,
- transFn = trans:x -> "proc { #{x} }"
+ trans*:rules -> { "Class.new(#{sName}) do\n@name = #{name.inspect}\n#{rules * "\n"}end" },
+ locals = [anything+:vs] -> { vs.map { |v| "#{v} = " }.join('') + 'nil' }
+ | [] -> { '' },
+ transFn = trans:x -> { "proc { #{x} }" }
}

0 comments on commit a3b2f25

Please sign in to comment.