Package tool.model.grammar

Source Code of tool.model.grammar.TestLiteral

package tool.model.grammar;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.junit.Before;
import org.junit.Test;

import tool.model.grammar.ForteParser.literal_return;

public class TestLiteral {
  private ForteParser parser;

  private TokenStream getStream(File file) throws IOException{
    CharStream stream =
        new NoCaseFileStream(file.getAbsolutePath());
    ForteLexer lexer = new ForteLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    return tokenStream;
  }
  private TokenStream getStream(String source) throws IOException{
    CharStream stream =
        new NoCaseStringStream(source);
    ForteLexer lexer = new ForteLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    return tokenStream;
  }
  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);
      }
    }
  }

  @Before
  public void setUp() throws Exception {
    parser = new ForteParser(null);
  }

  @Test
  public void testStringLiteral(){
    testLiteral("'SELECT  SUM(ledger_acc_bal)\n'");
  }
  @Test
  public void testIntegerLiteral(){
    testLiteral("9");
  }
  @Test
  public void testFloatLiteral(){
    testLiteral("9.");
  }
  @Test
  public void testFloatWithZeroLiteral(){
    testLiteral("9.0");
  }
  @Test
  public void testBooleanTrueLiteral(){
    testLiteral("tRuE");
  }
  @Test
  public void testBooleanFalseLiteral(){
    testLiteral("FaLsE");
  }
  @Test
  public void testStringWithQuote(){
    testLiteral("'\\''");
  }
 
  public void testLiteral(String source) {
    //fail("Not yet implemented");
    try {
      ErrorReporter parseErrors = new ErrorReporter();
      CommonTree tree = null;
      CommonTokenStream tokens = (CommonTokenStream) getStream(source);
      parser.setTokenStream(tokens);
      parser.setErrorReporter(parseErrors);
      parser.setTreeAdaptor(new CommonTreeAdaptor(){
        public Object create(Token payload){
          return new CommonTree(payload);
        }
      });
      literal_return result = parser.literal();
      if (parser.getNumberOfSyntaxErrors() > 0){
        System.out.println(parser.getNumberOfSyntaxErrors() + " Syntax error(s)\n"
            + parseErrors.toString());
      }

      tree = (CommonTree) result.getTree();
      printTree(tree, 0);
      CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
      nodes.setTokenStream(tokens);
      ForteAST walker = new ForteAST(nodes);
      walker.literal();

    } catch (RecognitionException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of tool.model.grammar.TestLiteral

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.