// this file was being used to bootstrap a ruby ometa compiler. feel free to remove it ometa RubyOMetaTranslator { trans [:t apply(t):ans] -> ans, App 'super' anything+:args -> [self.sName, '._superApplyWithArgs(rt,', args.join(','), ')'].join(''), App :rule anything+:args -> ['rt._applyWithArgs("', rule, '",', args.join(','), ')'].join(''), App :rule -> ['rt._apply("', rule, '")'] .join(''), Act :expr -> expr, Pred :expr -> ['rt._pred(', expr, ')'] .join(''), Or transFn*:xs -> ['rt._or(', xs.join(','), ')'] .join(''), And notLast(#trans)*:xs trans:y -> { xs.push('return ' + y) ['(proc{', xs.join(';'), '}).call'].join('') }, And -> '(proc{})', Many trans:x -> ['rt._many(proc{return ', x, '})'] .join(''), Many1 trans:x -> ['rt._many1(proc{return ', x, '})'] .join(''), Set :n trans:v -> [n, '=', v].join(''), Not trans:x -> ['rt._not(proc{return ', x, '})'] .join(''), Lookahead trans:x -> ['rt._lookahead(proc{return ', x, '})'] .join(''), Form trans:x -> ['rt._form(proc{return ', x, '})'] .join(''), Rule :name locals:ls trans:body -> ['def rule_', name, '\n', ls, '\nreturn ', body, '\nend\n'] .join(''), Grammar :name :sName !(self.sName = sName) trans*:rules -> ['class ', name, ' < ', sName, '\n', rules.join('\n'), 'end'] .join(''), locals = [string+:vs] -> ['rt = Runtime'] .join('') | [] -> '', transFn = trans:x -> ['(proc{return ', x, '})'] .join('') } // asdf ometa OMetaParser <: Parser { nameFirst = '_' | '$' | letter, nameRest = nameFirst | digit, tsName = firstAndRest(#nameFirst, #nameRest):xs -> xs.join(''), name = spaces tsName, eChar = '\\' char:c -> unescape('\\' +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(BSJSParser, `expr):r foreign(BSJSTranslator, `trans, r), atomicHostExpr = foreign(BSJSParser, `semAction):r foreign(BSJSTranslator, `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 -> { self.locals.push(n); [`Set, n, x] } | empty -> x ) | ":" name:n -> { self.locals.push(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] | spaces (characters | sCharacters | string | number) | "[" expr:x "]" -> [`Form, x] | "(" expr:x ")" -> x, ruleName = name | spaces tsString, rule = &(ruleName:n) !(self.locals = ['$elf=this']) rulePart(n):x ("," rulePart(n))*:xs -> [`Rule, n, self.locals, [`Or, x].concat(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) } parseAndCompileGrammar = function(g) { return RubyOMetaTranslator.match(OMetaParser.matchAll(g, "grammar"), "trans") } // a bigger example thisFile = readFile("Ruby_Translator") ometaGrammar = thisFile.substring(0, thisFile.indexOf("}\n\n//") + 1) parseAndCompileGrammar(ometaGrammar)