org.antlr.runtime.ANTLRInputStream Java Examples

The following examples show how to use org.antlr.runtime.ANTLRInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #2
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #3
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #4
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #5
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #6
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #7
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #8
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #9
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #10
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #11
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #12
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #13
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #14
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #15
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #16
Source File: MoqlParser.java    From moql with Apache License 2.0 6 votes vote down vote up
public static ConditionMetadata parseCondition(String condition)
    throws MoqlException {
  Validate.notEmpty(condition, "Parameter 'condition' is empty!");
  try {
    ANTLRInputStream is = new ANTLRInputStream(new ByteArrayInputStream(
        condition.getBytes()));
    FilterLexer lexer = new FilterLexer(is);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    FilterParser parser = new FilterParser(tokens);
    return new ConditionMetadata(parser.searchCondition());
  } catch (Exception e) {
    // TODO Auto-generated catch block
    throw new MoqlException(StringFormater.format(
        "Parse condition '{}' failed!", condition), e);
  }
}
 
Example #17
Source File: MoqlParser.java    From moql with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param moql
 * @return SelectorMetadata or SetlectorMetadata
 */
public static SelectorDefinition parseMoql(String moql) throws MoqlException {
  Validate.notEmpty(moql, "Parameter 'moql' is empty!");
  try {
    ANTLRInputStream is = new ANTLRInputStream(new ByteArrayInputStream(
        moql.getBytes()));
    SelectorLexer lexer = new SelectorLexer(is);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SelectorParser parser = new SelectorParser(tokens);
    return parser.selector();
  } catch (Exception e) {
    // TODO Auto-generated catch block
    throw new MoqlException(StringFormater.format("Parse moql '{}' failed!",
        moql), e);
  }
}
 
Example #18
Source File: OperandFactoryImpl.java    From moql with Apache License 2.0 6 votes vote down vote up
@Override
public Operand createOperand(String operand) throws MoqlException {
	// TODO Auto-generated method stub
	Validate.notEmpty(operand, "Parameter 'operand' is empty!");
	Operand pseudoOperand = createPseudoOperand(operand);
	if (pseudoOperand != null)
	  return pseudoOperand;
	try {
		ANTLRInputStream is = new ANTLRInputStream(new ByteArrayInputStream(operand.getBytes()));
		OperandLexer lexer = new OperandLexer(is);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		OperandParser parser = new OperandParser(tokens);
		parser.setFunctionFactory(functionFactory);
		return parser.operand();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		throw new MoqlException(
         StringFormater.format("Create operand '{}' failed!", operand), e);
	}
}
 
Example #19
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */

    public void loadGroupFile(String prefix, String fileName) {
        if ( verbose ) System.out.println(this.getClass().getSimpleName()+".loadGroupFile(group-file-prefix="+prefix +
                                          ", fileName=" +
fileName +
                                          ")");
        GroupParser parser;
        try {
            URL f = new URL(fileName);
            ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
            GroupLexer lexer = new GroupLexer(fs);
            fs.name = fileName;
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            parser = new GroupParser(tokens);
            parser.group(this, prefix);
        }
        catch (Exception e) {
            errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
        }
    }
 
Example #20
Source File: STGroup.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Load a group file with full path {@code fileName}; it's relative to root by {@code prefix}. */
public void loadGroupFile(String prefix, String fileName) {
	if ( verbose ) System.out.println(this.getClass().getSimpleName()+
									  ".loadGroupFile(group-file-prefix="+prefix+", fileName="+fileName+")");
	GroupParser parser;
	try {
		URL f = new URL(fileName);
		ANTLRInputStream fs = new ANTLRInputStream(f.openStream(), encoding);
		GroupLexer lexer = new GroupLexer(fs);
		fs.name = fileName;
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		parser = new GroupParser(tokens);
		parser.group(this, prefix);
	}
	catch (Exception e) {
		errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName);
	}
}
 
Example #21
Source File: ParsedModule.java    From swift-t with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the specified file and create a ParsedModule object
 * @param path
 * @param preprocessed
 * @return
 * @throws IOException
 */
public static ParsedModule parse(String moduleName, String path,
                                 boolean preprocessed) throws IOException {
  FileInputStream inputStream = setupInput(path);
  /* Parse the input file and build AST */
  ANTLRInputStream antlrInput = new ANTLRInputStream(inputStream);
  LineMapping lineMapping;
  if (preprocessed) {
    int startMark = antlrInput.mark();
    lineMapping = parsePreprocOutput(antlrInput);
    antlrInput.rewind(startMark);
  } else {
    // Treat # lines as comments.  All input from same file
    lineMapping = LineMapping.makeSimple(path);
  }
  SwiftAST tree = runANTLR(antlrInput, lineMapping);

  return new ParsedModule(moduleName, path, tree, lineMapping);
}
 
Example #22
Source File: FIS.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Load an FCL file and create a "Fuzzy inference system (FIS)"  
 * @param fileName : FCL file name
 * @param verbose : Be verbose?
 * @return A new FIS or null on error
 */
public static FIS load(InputStream inputStream, boolean verbose) {
	// Parse file (lexer first, then parser)
	FclLexer lexer;
	try {
		lexer = new FclLexer(new ANTLRInputStream(inputStream));
	} catch (IOException e1) {
		System.err.println("Error reading inputStream'" + inputStream + "'");
		return null;
	}

	// Parse tree and create FIS
	try {
		return createFromLexer(lexer, verbose);
	} catch (RecognitionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #23
Source File: DRLFactory.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static DRLParser buildParser( final InputStream is, final String encoding, LanguageLevelOption languageLevel ) {
    try {
        return buildParser(encoding != null ? new ANTLRInputStream(is, encoding) : new ANTLRInputStream(is), languageLevel);
    } catch ( final Exception e ) {
        throw new RuntimeException( "Unable to parser Reader", e );
    }
}
 
Example #24
Source File: AbstractParser.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public static void load(InputStream in, Proto proto) throws Exception
{
    // Create an input character stream from standard in
    ANTLRInputStream input = new ANTLRInputStream(in);
    // Create an ExprLexer that feeds from that stream
    ProtoLexer lexer = new ProtoLexer(input);
    // Create a stream of tokens fed by the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that feeds off the token stream
    ProtoParser parser = new ProtoParser(tokens);
    // Begin parsing at rule prog
    parser.parse(proto);
}
 
Example #25
Source File: CMIS_FTSTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testLexerOutput() throws IOException
{
    String str = "~woof^2";
    CharStream input = new ANTLRInputStream(new ByteArrayInputStream(str.getBytes("UTF-8")));
    FTSLexer lexer = new FTSLexer(input);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    for(CommonToken token : (List<CommonToken>)tokenStream.getTokens())
    {
        System.out.println(token.toString());
    }
    
}
 
Example #26
Source File: FTSTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testLexer() throws IOException, RecognitionException
{
    ClassLoader cl = FTSTest.class.getClassLoader();
    InputStream modelStream = cl.getResourceAsStream("org/alfresco/repo/search/impl/parsers/fts_test.gunit");

    CharStream input = new ANTLRInputStream(modelStream);

    gUnitExecutor executer = new gUnitExecutor(parse(input), "FTS");

    String result = executer.execTest();
    System.out.print(executer.execTest()); // unit test result

    assertEquals("Failures: " + result, 0, executer.failures.size());
    assertEquals("Invalids " + result, 0, executer.invalids.size());
}
 
Example #27
Source File: DRLFactory.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public static DRLLexer buildLexer( final InputStream is, final String encoding, LanguageLevelOption languageLevel ) {
    try {
        return getDRLLexer(encoding != null ? new ANTLRInputStream(is, encoding) : new ANTLRInputStream(is), languageLevel);
    } catch ( final Exception e ) {
        throw new RuntimeException( "Unable to parser Reader", e );
    }
}
 
Example #28
Source File: ParsedModule.java    From swift-t with Apache License 2.0 4 votes vote down vote up
/**
   Use ANTLR to parse the input and get the Tree
 * @throws IOException
 */
private static SwiftAST runANTLR(ANTLRInputStream input, LineMapping lineMap) {

  ExMLexer lexer = new ExMLexer(input);
  lexer.lineMap = lineMap;
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  ExMParser parser = new ExMParser(tokens);
  parser.lineMap = lineMap;
  parser.setTreeAdaptor(new SwTreeAdaptor());

  // Launch parsing
  ExMParser.program_return program = null;
  try
  {
    program = parser.program();
  }
  catch (RecognitionException e)
  {
    // This is an internal error
    e.printStackTrace();
    System.out.println("Parsing failed: internal error");
    throw new STCFatal(ExitCode.ERROR_INTERNAL.code());
  }

  /* NOTE: in some cases the antlr parser will actually recover from
   *    errors, print an error message and continue, generating the
   *    parse tree that it thinks is most plausible.  This is where
   *    we detect this case.
   */
  if (parser.parserError) {
    // This is a user error
    System.err.println("Error occurred during parsing.");
    throw new STCFatal(ExitCode.ERROR_USER.code());
  }

  // Do we actually need this check? -Justin (10/26/2011)
  if (program == null)
    throw new STCRuntimeError("PARSER FAILED!");


  SwiftAST tree = (SwiftAST) program.getTree();

  return tree;
}
 
Example #29
Source File: ParsedModule.java    From swift-t with Apache License 2.0 4 votes vote down vote up
/**
 * Use the file and line info from c preprocessor to
 * update SwiftAST
 * @param lexer
 * @param tree
 */
private static LineMapping parsePreprocOutput(ANTLRInputStream input) {

  /*
   * This function is a dirty hack, but works ok
   * because the C preprocessor output has a very simple output format
   * of
   * # linenum filename flags
   *
   * We basically just need the linenum and filename
   * (see http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html)
   */
  LineMapping posTrack = new LineMapping();
  try {
    ExMLexer lexer = new ExMLexer(input);
    /*
     * don't emit error messages with bad line numbers:
     * we will emit lexer error messages on the second pass
     */
    lexer.quiet = true;
    Token t = lexer.nextToken();
    while (t.getType() != ExMLexer.EOF) {
      if (t.getChannel() == ExMLexer.CPP) {
        assert(t.getText().substring(0, 2).equals("# ")): t.getText();
        StreamTokenizer tok = new StreamTokenizer(
              new StringReader(t.getText().substring(2)));
        tok.slashSlashComments(false);
        tok.slashStarComments(false);
        tok.quoteChar('"');
        if (tok.nextToken() != StreamTokenizer.TT_NUMBER) {
          throw new STCRuntimeError("Confused by " +
              " preprocessor line " + t.getText());
        }
        int lineNum = (int)tok.nval;

        if (tok.nextToken() == '"') {
          // Quoted file name with octal escape sequences

          // Ignore lines from preprocessor holding information we
          // don't need (these start with "<"
          String fileName = tok.sval;
          if (!fileName.startsWith("<")) {
            posTrack.addPreprocInfo(t.getLine() + 1,
                                  fileName, lineNum);
          }
        }
      }
      t = lexer.nextToken();
    }
  } catch (IOException e) {
    System.out.println("Error while trying to read preprocessor" +
        " output: " + e.getMessage());
    throw new STCFatal(1);
  }
  return posTrack;
}
 
Example #30
Source File: ProtoUtil.java    From protostuff with Apache License 2.0 4 votes vote down vote up
/**
 * Loads the proto from an {@link InputStream}.
 */
public static void loadFrom(InputStream in, Proto target) throws Exception
{
    loadFrom(new ANTLRInputStream(in), target);
}