| 5 | import stone.ast.*; |
| 6 | |
| 7 | public class BasicParser { |
| 8 | HashSet<String> reserved = new HashSet<String>(); |
| 9 | Operators operators = new Operators(); |
| 10 | Parser expr0 = rule(); |
| 11 | Parser primary = rule(PrimaryExpr.class) |
| 12 | .or(rule().sep("(").ast(expr0).sep(")"), |
| 13 | rule().number(NumberLiteral.class), |
| 14 | rule().identifier(Name.class, reserved), |
| 15 | rule().string(StringLiteral.class)); |
| 16 | Parser factor = rule().or(rule(NegativeExpr.class).sep("-").ast(primary), |
| 17 | primary); |
| 18 | Parser expr = expr0.expression(BinaryExpr.class, factor, operators); |
| 19 | |
| 20 | Parser statement0 = rule(); |
| 21 | Parser block = rule(BlockStmnt.class) |
| 22 | .sep("{").option(statement0) |
| 23 | .repeat(rule().sep(";", Token.EOL).option(statement0)) |
| 24 | .sep("}"); |
| 25 | Parser simple = rule(PrimaryExpr.class).ast(expr); |
| 26 | Parser statement = statement0.or( |
| 27 | rule(IfStmnt.class).sep("if").ast(expr).ast(block) |
| 28 | .option(rule().sep("else").ast(block)), |
| 29 | rule(WhileStmnt.class).sep("while").ast(expr).ast(block), |
| 30 | simple); |
| 31 | |
| 32 | Parser program = rule().or(statement, rule(NullStmnt.class)) |
| 33 | .sep(";", Token.EOL); |
| 34 | |
| 35 | public BasicParser() { |
| 36 | reserved.add(";"); |
| 37 | reserved.add("}"); |
| 38 | reserved.add(Token.EOL); |
| 39 | |
| 40 | operators.add("=", 1, Operators.RIGHT); |
| 41 | operators.add("==", 2, Operators.LEFT); |
| 42 | operators.add(">", 2, Operators.LEFT); |
| 43 | operators.add("<", 2, Operators.LEFT); |
| 44 | operators.add("+", 3, Operators.LEFT); |
| 45 | operators.add("-", 3, Operators.LEFT); |
| 46 | operators.add("*", 4, Operators.LEFT); |
| 47 | operators.add("/", 4, Operators.LEFT); |
| 48 | operators.add("%", 4, Operators.LEFT); |
| 49 | } |
| 50 | public ASTree parse(Lexer lexer) throws ParseException { |
| 51 | return program.parse(lexer); |
| 52 | } |
| 53 | } |
nothing calls this directly
no test coverage detected