aboutsummaryrefslogtreecommitdiffstats
path: root/main/core/parser/Parser.java
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2016-06-28 21:09:43 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2016-07-26 19:47:12 +0300
commit8f84c00978dc05007873a9fe1de6a18511dc3cf8 (patch)
tree39fead540e22addee40b95b68db08cbea620745b /main/core/parser/Parser.java
parent48dcd488054024d98fe1e659a4029630130d1d35 (diff)
Restructure project for use with maven
Diffstat (limited to 'main/core/parser/Parser.java')
-rw-r--r--main/core/parser/Parser.java108
1 files changed, 0 insertions, 108 deletions
diff --git a/main/core/parser/Parser.java b/main/core/parser/Parser.java
deleted file mode 100644
index f6e1208..0000000
--- a/main/core/parser/Parser.java
+++ /dev/null
@@ -1,108 +0,0 @@
-package com.jantuomi.interpreter.main.core.parser;
-
-import com.jantuomi.interpreter.main.core.parser.ast.ASTNode;
-import com.jantuomi.interpreter.main.core.tokenizer.token.ArgumentInfo;
-import com.jantuomi.interpreter.main.core.tokenizer.token.Token;
-import com.jantuomi.interpreter.main.exception.ExceptionManager;
-import com.jantuomi.interpreter.main.exception.InterpreterException;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Stack;
-
-/**
- * Created by jan on 11.6.2016.
- */
-public class Parser {
- private static final Parser instance = new Parser();
-
- private List<Token> tokens;
- private List<ASTNode> statementSequence;
-
- public static Parser getInstance() {
- return instance;
- }
-
- private Parser() {
-
- }
-
- private Stack<Token> stack;
-
- public List<Token> parse(List<Token> tokens) throws InterpreterException {
- this.tokens = tokens;
- this.stack = new Stack<>();
-
- List<Token> args;
- List<Token> output = new ArrayList<>();
- for (Token t : tokens) {
- if (t.getTokenType() == Token.Type.NewlineToken) {
-// if (stack.size() > 0) {
-// output.add(stack.pop());
-// }
- continue;
- }
-
- args = new ArrayList<>();
- ArgumentInfo argumentInfo = t.getArgumentInfo();
-
- /*
- This branch is executed if the token has a variable list of arguments
- */
- if (argumentInfo.getVarargs()) {
- while (true) {
- if (stack.size() > 0) {
- Token arg = stack.pop();
- if (arg.getTokenType() != argumentInfo.getTerminator()) {
- args.add(arg);
- }
- else {
- args.add(arg);
- break;
- }
- }
- }
- }
- /*
- If the token has an optional argument type (it is only an argument if
- it is of certain type)
- */
- else if (argumentInfo.getOptionalArgument() != null) {
- if (stack.size() > 0 && stack.peek().getTokenType() == argumentInfo.getOptionalArgument()) {
- args.add(stack.pop());
- }
- }
- /* Normal cases with a fixed list of arguments */
- else {
- for (int i = 0; i < argumentInfo.getCount(); i++) {
- if (stack.size() > 0) {
- args.add(stack.pop());
- } else {
- ExceptionManager.raise(InterpreterException.ExceptionType.ArgumentError, t.getLine(),
- t.toString(), Integer.toString(argumentInfo.getCount()));
- return null;
- }
- }
- }
-
- Token result = t.setArguments(args);
- stack.push(result);
- }
- while (stack.size() > 0) {
- output.add(stack.pop());
- }
- return output;
- }
-
- public void printTree(Token e) {
- System.out.println("### Token Tree begin ###");
- e.print(0);
- System.out.println("### Token Tree end ###");
- }
-
- public void printAllTrees(List<Token> trees) {
- for (Token tree : trees) {
- printTree(tree);
- }
- }
-}