Navigation
Synopsis Parse input text (from a string or a location) and return a parse tree.
Function
  1. &T<:Tree parse(type[&T<:Tree] begin, str input)
  2. &T<:Tree parse(type[&T<:Tree] begin, str input, loc origin)
  3. &T<:Tree parse(type[&T<:Tree] begin, loc input)
Usage import ParseTree;
Description
  1. Parse a string and return a parse tree.
  2. Parse a string and return a parse tree, origin defines the original location of the input.
  3. Parse the contents of resource input and return a parse tree.
Examples
rascal>import demo::lang::Exp::Concrete::NoLayout::Syntax;
ok
rascal>import ParseTree;
ok
Seeing that parse returns a parse tree:
rascal>parse(#Exp, "2+3");
sort("Exp"): `2+3`
Tree: appl(prod(sort("Exp"),[sort("Exp"),layouts("default"),lit("+"),layouts("default"),sort("Exp")],{assoc(left())}),[appl(prod(sort("Exp"),[lex("IntegerLiteral")],{}),[appl(prod(lex("IntegerLiteral"),[iter(\char-class([range(48,57)]))],{}),[appl(regular(iter(\char-class([range(48,57)]))),[char(50)])[@loc=|file://-|(0,1,<1,0>,<1,1>)]])[@loc=|file://-|(0,1,<1,0>,<1,1>)]])[@loc=|file://-|(0,1,<1,0>,<1,1>)],appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(1,0,<1,1>,<1,1>)],appl(prod(lit("+"),[\char-class([range(43,43)])],{}),[char(43)]),appl(prod(layouts("default"),[],{}),[])[@loc=|file://-|(2,0,<1,2>,<1,2>)],appl(prod(sort("Exp"),[lex("IntegerLiteral")],{}),[appl(prod(lex("IntegerLiteral"),[iter(\char-class([range(48,57)]))],{}),[appl(regular(iter(\char-class([range(48,57)]))),[char(51)])[@loc=|file://-|(2,1,<1,2>,<1,3>)]])[@loc=|file://-|(2,1,<1,2>,<1,3>)]])[@loc=|file://-|(2,1,<1,2>,<1,3>)]])[@loc=|file://-|(0,3,<1,0>,<1,3>)]
Catching a parse error:
rascal>import IO;
ok
rascal>try {
>>>>>>>  Exp e = parse(#Exp, "[email protected]");
>>>>>>>}
>>>>>>>catch ParseError(loc l): {
>>>>>>>  println("I found a parse error at line <l.begin.line>, column <l.begin.column>");
>>>>>>>}
I found a parse error at line 1, column 1
ok
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.