How to use the antlr4.CommonTokenStream function in antlr4

To help you get started, we’ve selected a few antlr4 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cancerberoSgx / univac / src / getAst.ts View on Github external
function getAst(options: GetAstOptions) {
  const input = options.input
  var MyGrammarLexer = require('./grammar/CLexer').CLexer
  var MyGrammarParser = require('./grammar/CParser').CParser
  // var MyGrammarListener = require('./grammar/CListener').CListener;
  var chars = new antlr4.InputStream(input)
  var lexer = new MyGrammarLexer(chars)
  var tokens = new antlr4.CommonTokenStream(lexer)
  var parser = new MyGrammarParser(tokens)
  //  parser.buildParseTrees = true;
  // //  parser.query()
  var tree = parser.compilationUnit()
  let n: Partial = {
    children: []
  }
  class Visitor {
    visitChildren(ctx: Ctx) {
      if (!ctx) {
        return
      }
      let node = this.getNode(ctx)
      n.children!.push(node)
      // n = mode
      // this.parentNode = node
github ballerina-attic / composer / modules / web / js / ballerina / parser / parser.js View on Github external
Parser.prototype.parse = function(input){
    // setup parser
    var chars = new antlr4.InputStream(input);
    var lexer = new BallerinaLexer.BallerinaLexer(chars);
    var tokens  = new antlr4.CommonTokenStream(lexer);
    var parser = new BallerinaParser.BallerinaParser(tokens);
    var listener = new BLangParserListener(parser);

    // set custom error listener for collecting syntax errors
    var errorListener = new BLangParserErrorListener();
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);

    // start parsing
    var tree = parser.compilationUnit();

    antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);

    // return collected errors
    return errorListener.getErrors();
};
github mongodb-js / bson-transpilers / index.js View on Github external
const loadPyTree = (input, start) => {
  const chars = new antlr4.InputStream(input + '\n'); // requires newline
  const lexer = new Python3Lexer.Python3Lexer(chars);

  const tokens = new antlr4.CommonTokenStream(lexer);
  const parser = new Python3Parser.Python3Parser(tokens);
  parser.buildParseTrees = true;

  const listener = new ErrorListener();
  parser.removeErrorListeners(); // Remove the default ConsoleErrorListener
  parser.addErrorListener(listener); // Add back a custom error listener

  return parser[start]();
};
github merklejerk / solpp / src / antlr-utils.js View on Github external
function createParseTree(LexerType, ParserType, startRule, text, mode=null) {
	const errors = new ErrorListener();
	const lexer = new LexerType(antlr.CharStreams.fromString(text));
	// If mode was passed, jump straight into that mode.
	if (_.isString(mode) && _.includes(lexer.modeNames, mode))
		lexer.mode(_.indexOf(lexer.modeNames, mode));
	const tokens = new antlr.CommonTokenStream(lexer);
	const parser = new ParserType(tokens);
	parser.removeErrorListeners();
	lexer.removeErrorListeners();
	parser.addErrorListener(errors);
	lexer.addErrorListener(errors);
	parser.buildParseTrees = true;
	return parser[startRule]();
}
github ballerina-attic / composer / modules / web / js / ballerina / parser / parser.js View on Github external
*
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var antlr4 = require('antlr4');
var BallerinaLexer = require('./BallerinaLexer');
var BallerinaParser = require('./BallerinaParser');

var input = "import com.ballerina.test;";
var chars = new antlr4.InputStream(input);
var lexer = new BallerinaLexer.BallerinaLexer(chars);
var tokens  = new antlr4.CommonTokenStream(lexer);
var parser = new BallerinaParser.BallerinaParser(tokens);
parser.compilationUnit();
github protofire / solhint / lib / index.js View on Github external
function processStr(inputStr, config = {}, fileName = '') {
  const chars = new antlr4.InputStream(inputStr)
  const lexer = new SolidityLexer(chars)
  const tokens = new antlr4.CommonTokenStream(lexer)
  const parser = new SolidityParser(tokens)
  parser.buildParseTrees = true

  const tree = parser.sourceUnit()
  const reporter = new Reporter(tokens, config)

  const listener = new TreeListener(checkers(reporter, config, inputStr, fileName))
  antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree)

  return reporter
}
github solidity-ide / antlr-parser / src / SolidityParser.ts View on Github external
private parseStream (stream: any): void {

        if (this.isComplete) {
            throw new EvalError("The Observable has been completed.");
        }

        const lexer = new Lexer(stream);
        const tokens = new antlr4.CommonTokenStream(lexer);
        const parser = new Parser(tokens);
        parser.buildParseTrees = true;

        parser.removeErrorListeners();
        parser.addErrorListener(this.errorListener);

        const tree = parser.sourceUnit();
        antlr4.tree.ParseTreeWalker.DEFAULT.walk(this.ruleListener, tree);
        parser.removeErrorListeners();

        if (this.fireComplete) {
            this.complete();
        }
    }
}
github kitspace / electro-grammar / js / lib / lax_parser.js View on Github external
function parse(input) {
    const chars = new antlr4.InputStream(input);
    const lexer = new ElectroGrammarLexer(chars);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new ElectroGrammarParser(tokens);
    parser.buildParseTrees = true;

    const tree = parser[start_rule]();
    antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree);
    return listener.obj;
  }
  return parse;
github daud-io / daud / Game.Engine / wwwroot / src / parser / parseTheme.js View on Github external
function parseCssIntoRules(css) {
    const chars = new antlr4.InputStream(css);
    const lexer = new ScssLexer(chars);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new ScssParser(tokens);
    const tree = parser.stylesheet();

    const ruleList = [];
    function addRulesFromStatement(statement, rules) {
        const selectors = statement.ruleset().selectors();
        const block = statement.ruleset().block();

        const blockProps = block.property().map((x, i) => {
            return [
                block.property(i).identifier().getText(),
                block
                    .property(i)
                    .values()
                    .children.map((x) => (x.children ? x.children.map((y) => y.getText()).join(" ") : x.getText()))
                    .filter((y) => y !== ","),
github indeedeng / iql / iql2 / src / js / Parsers.js View on Github external
const chars = new antlr4.InputStream(input);
    const originalLA = chars.LA;
    chars.LA = function(x) {
        const result = originalLA.call(this, x);
        if (result <= 0) {
            return result;
        } else {
            return String.fromCharCode(result).toUpperCase().charCodeAt(0);
        }
    };

    const lexer = new JQLLexer.JQLLexer(chars);
    const errorListener = new CollectingErrorListener(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(errorListener);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new JQLParser.JQLParser(tokens);
    parser.buildParseTrees = true;
    parser.removeErrorListeners();
    parser.addErrorListener(errorListener);
    const result = parser[parserName].apply(parser, parserArgs || []);
    if (errorListener.errors.length > 0) {
        return failure(errorListener.errors, errorListener.expected, result);
    } else if (result.parser._input.index === 0) {
        return failure("consumed no tokens -- first token likely invalid", [], result);
    } else {
        return success(result);
    }
}