Bamboo

boo, ometa and extensible parsing I

Meanwhile in a repository not far away:

namespace metaboo

import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
import Boo.OMeta
import Boo.OMeta.Parser

syntax Units:
  atom = mass | super
  mass = (integer >> value as Expression, "kg") ^ [| Mass($value, "kg") |]

syntax Ranges:
  atom = integer_range | super
  integer_range = (
      integer >> begin as Expression, DOT, DOT,
      integer >> end as Expression) ^ [| range($begin, $end) |]

def parse(code as string):
  compiler = BooCompiler()
  compiler.Parameters.References.Add(
    System.Reflection.Assembly.GetExecutingAssembly())
  compiler.Parameters.Input.Add(IO.StringInput("code", code))
  compiler.Parameters.Pipeline = CompilerPipeline()
  compiler.Parameters.Pipeline.Add(BooParserStep())
  return compiler.Run()

code = """
import metaboo.Units
import metaboo.Ranges
a = 3kg
print a
for i in 1..3:
  print i
"""

result = parse(code)
assert 0 == len(result.Errors), result.Errors.ToString()
assert 1 == len(result.CompileUnit.Modules)
print result.CompileUnit.Modules[0].ToCodeString()

And the output is, of course:

import metaboo.Units
import metaboo.Ranges

a = Mass(3, 'kg')
print a
for i in range(1, 3):
    print i

Share this: