Package tool.model.grammar

Source Code of tool.model.grammar.StringLiteralWithQuote

package tool.model.grammar;

import static org.junit.Assert.*;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.junit.Before;
import org.junit.Test;

import tool.model.grammar.ForteParser.expression_return;
import tool.model.grammar.ForteParser.literal_return;
import tool.model.grammar.ForteParser.qualifiedIdentExpression_return;
import tool.model.grammar.ForteParser.qualifiedName_return;
import tool.model.grammar.ForteParser.statement_return;

public class StringLiteralWithQuote {

  @Before
  public void setUp() throws Exception {
  }

  private static final boolean PRINT_TREE = false;
  private ErrorReporter reporter;
  private void failOnSyntaxError(String string, ForteParser parser, ForteAST ast){
    int errors = 0;
    int treeErrors = 0;
    errors = parser.getNumberOfSyntaxErrors();
    if (errors > 0 && this.reporter != null){
      System.out.println(this.reporter.toString());
    }
    if (ast != null)
      treeErrors = ast.getNumberOfSyntaxErrors();
    System.out.println(string + " syntax errors: " + errors + " tree errors: " + treeErrors);
    if (errors + treeErrors> 0){
      fail("Syntax errors: " + errors);
    }
  }
  private void printTree(CommonTree tree, int indent){
    if (tree != null){
      StringBuffer sb = new StringBuffer(indent);
      for (int i = 0; i < indent; i++)
        sb = sb.append("  ");
      for (int i = 0; i < tree.getChildCount(); i++){
        System.out.println(sb.toString() + tree.getChild(i).toString());
        printTree((CommonTree)tree.getChild(i), indent+1);
      }
    }
  }
  public int testStatement(String name, String source) throws Exception{
    int errors = 0;
    ForteAST walker = null;
    ForteParser parser = null;
    System.out.println("===== Statement: " + name + " =====");
    CharStream stream =
        new NoCaseStringStream(source);
    ForteLexer lexer = new ForteLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
   
    parser = new ForteParser(tokens);
    reporter = new ErrorReporter();
    parser.setErrorReporter(reporter);
//    parser.setTokenStream(tokens);
    statement_return result = parser.statement();
    CommonTree tree = (CommonTree) result.getTree();
    if (PRINT_TREE)
        System.out.println("Tree==>"+tree.toStringTree());
//      printTree(tree, 0);
    errors = parser.getNumberOfSyntaxErrors();

    if (errors == 0){
      CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
      nodes.setTokenStream(tokens);
      walker = new ForteAST(nodes);
      walker.statement();
    }
    failOnSyntaxError(name, parser, walker);
    return errors;
  }
  public int testExpression(String name, String source) throws Exception{
    int errors = 0;
    ForteAST walker = null;
    ForteParser parser = null;
    try {
      System.out.println("===== Expression: " + name + " =====");
      CharStream stream =
          new NoCaseStringStream(source);
      ForteLexer lexer = new ForteLexer(stream);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      parser = new ForteParser(tokens);
      reporter = new ErrorReporter();
      parser.setErrorReporter(reporter);
//      parser.setTokenStream(tokens);
      expression_return result = parser.expression();
      CommonTree tree = (CommonTree) result.getTree();
      errors = parser.getNumberOfSyntaxErrors();

      if (errors == 0){
        if (PRINT_TREE)
          System.out.println("Tree==>"+tree.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
        nodes.setTokenStream(tokens);
        walker = new ForteAST(nodes);
        walker.expression();
      }
    } finally {
      failOnSyntaxError(name, parser, walker);
    }
    return errors;
  }

  public int testQualifiedName(String source) throws Exception{
    int errors = 0;
    ForteAST walker = null;
    ForteParser parser = null;
    try {
      System.out.println("===== QualifiedName: " + source + " =====");
      CharStream stream =
          new NoCaseStringStream(source);
      ForteLexer lexer = new ForteLexer(stream);
      TokenStream tokens = new CommonTokenStream(lexer);
      reporter = new ErrorReporter();
      parser = new ForteParser(tokens);
      parser.setErrorReporter(reporter);
//      parser.setTokenStream(tokens);
      qualifiedName_return result = parser.qualifiedName();
      CommonTree tree = (CommonTree) result.getTree();
      errors = parser.getNumberOfSyntaxErrors();

      if (errors == 0){
        if (PRINT_TREE)
          System.out.println("Tree==>"+tree.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
        nodes.setTokenStream(tokens);
        walker = new ForteAST(nodes);
        walker.qualifiedName();
      }
    } finally {
      failOnSyntaxError(source, parser, walker);
    }
    return errors;
  }
  public int testQualifiedIdentExpression(String source) throws Exception{
    int errors = 0;
    ForteAST walker = null;
    ForteParser parser = null;
    try {
      System.out.println("===== QualifiedName: " + source + " =====");
      CharStream stream =
          new NoCaseStringStream(source);
      ForteLexer lexer = new ForteLexer(stream);
      TokenStream tokens = new CommonTokenStream(lexer);
      parser = new ForteParser(tokens);
//      parser.setTokenStream(tokens);
      qualifiedIdentExpression_return result = parser.qualifiedIdentExpression();
      CommonTree tree = (CommonTree) result.getTree();
      errors = parser.getNumberOfSyntaxErrors();

      if (errors == 0){
        if (PRINT_TREE)
          System.out.println("Tree==>"+tree.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
        nodes.setTokenStream(tokens);
        walker = new ForteAST(nodes);
        //walker.qualifiedIdentExpression();
      }
    } finally {
      failOnSyntaxError(source, parser, walker);
    }
    return errors;
  }

  public int testLiteral(String source) throws Exception{
    int errors = 0;
    ForteAST walker = null;
    ForteParser parser = null;
    try {
      System.out.println("===== Literal: " + source + " =====");
      CharStream stream =
          new NoCaseStringStream(source);
      ForteLexer lexer = new ForteLexer(stream);
      TokenStream tokens = new CommonTokenStream(lexer);
      parser = new ForteParser(tokens);
//      parser.setTokenStream(tokens);
      literal_return result = parser.literal();
      CommonTree tree = (CommonTree) result.getTree();
      errors = parser.getNumberOfSyntaxErrors();

      if (errors == 0){
        if (PRINT_TREE)
          System.out.println("Tree==>"+tree.toStringTree());
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
        nodes.setTokenStream(tokens);
        walker = new ForteAST(nodes);
        //walker.qualifiedIdentExpression();
      }
    } finally {
      failOnSyntaxError(source, parser, walker);
    }
    return errors;
  }

  @Test
  public void literalEscapedQuoteLiteral() throws Exception{
    testLiteral("'\\''");
  }
  @Test
  public void literalEscapedQuoteExpression() throws Exception{
    testExpression("Quoted Expression","'\\''");
  }
  @Test
  public void literalAssignmentExpression() throws Exception{
    testExpression("Assignment Expression","bob = '\\''");
  }
  @Test
  public void literalAssignmentStatement() throws Exception{
//    System.out.println("-- cat ---");
//    testStatement("Assignment Statement","bob = 'at';");
    System.out.println("-- ' ---");
    testStatement("Assignment Statement","bob = '\\'';");
  }
  @Test
  public void statementEscapedQuote() throws Exception{
    testStatement("statementEscapedQuote", "lSingleQuote : String = '\\'';");
  }
 
  @Test
  public void lexerTest() throws Exception{
    CharStream stream =
        new NoCaseStringStream("bob = '\\'';");
    TestLexer lexer = new TestLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    for (Object token : tokens.getTokens()){
      CommonToken ct = (CommonToken)token;
      if (ct.getChannel() != 99)
        System.out.println(((CommonToken)token).getText());
    }
  }
  @Test
  public void lexerTest2() throws Exception{
    CharStream stream =
        new NoCaseStringStream("bob = '''';");
    ForteLexer lexer = new ForteLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    for (Object token : tokens.getTokens()){
      CommonToken ct = (CommonToken)token;
      if (ct.getChannel() != 99)
        System.out.println(((CommonToken)token).getText());
    }
  }

}
TOP

Related Classes of tool.model.grammar.StringLiteralWithQuote

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.