コード例 #1
0
ファイル: compile-lexer.js プロジェクト: zxqfox/smarty-parser
    var res = U.trycatch(function () {
      var buffer = U.cleanupTokens(String(file.contents));

      // parse grammar
      var lexerGrammar = LexerParser.parse(buffer);
      if (!lexerGrammar.options) {
        lexerGrammar.options = {};
      }
      lexerGrammar.options.moduleName = 'lexer'; // used exported variable
      lexerGrammar.options.moduleType = 'js';

      // var tokens = U.fetchTokens(res);

      return Lexer.generate(lexerGrammar);
    }, U.createError.bind(null, PLUGIN_NAME, file));
コード例 #2
0
ファイル: index.js プロジェクト: Jinkwon/alasql
    return through.obj(function (file, encoding, callback) {

        if (file.isNull()) {
            // Do nothing
        }

        if (file.isStream()) {
            // Not yet supported
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streams are not supported yet'));
        }

        if (file.isBuffer()) {

            var jlOptions = {
                moduleName: template(options.moduleName, file),
                moduleType: options.moduleType
            };

            file = file.clone();

            var outFile = template(options.outFile, file);
            file.path = outFile
                ? path.resolve(path.dirname(file.path), './', outFile)
                : gutil.replaceExtension(file.path, '.js');

            try {
                var grammar = file.contents.toString();
                if (options.json) {
                    grammar = JSON.parse(grammar);
                } else {
                    grammar = lexParser.parse(grammar);
                }

                grammar.options = extend(grammar.options, jlOptions);

                var lexer = jisonLex.generate(grammar);
                file.contents = new Buffer(lexer);
            } catch (err) {
                this.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
            }
        }

        this.push(file);

        return callback();
    });
コード例 #3
0
ファイル: regexp-lexer.js プロジェクト: toms-dev/jison-lex
// process the grammar and build final data structures and functions
function processGrammar(dict, tokens) {
    var opts = {};
    if (typeof dict === 'string') {
        dict = lexParser.parse(dict);
    }
    dict = dict || {};

    opts.options = dict.options || {};
    opts.moduleType = opts.options.moduleType;
    opts.moduleName = opts.options.moduleName;

    opts.conditions = prepareStartConditions(dict.startConditions);
    opts.conditions.INITIAL = {rules:[],inclusive:true};

    opts.performAction = buildActions.call(opts, dict, tokens);
    opts.conditionStack = ['INITIAL'];

    opts.moduleInclude = (dict.moduleInclude || '').trim();
    return opts;
}
コード例 #4
0
ファイル: cli.js プロジェクト: dahmian/jison
function processGrammar (file, lexFile, name) {
    var grammar;
    try {
        grammar = ebnfParser.parse(file);
    } catch (e) {
        try {
            grammar = JSON.parse(file);
        } catch (e2) {
            throw e;
        }
    }

    var settings = grammar.options || {};
    if (lexFile) grammar.lex = lexParser.parse(lexFile);
    settings.debug = opts.debug;
    if (!settings.moduleType) settings.moduleType = opts['module-type'];
    if (!settings.moduleName && name) settings.moduleName = name.replace(/-\w/g, function (match){ return match.charAt(1).toUpperCase(); });

    var generator = new jison.Generator(grammar, settings);
    return generator.generate(settings);
}
コード例 #5
0
ファイル: jison.js プロジェクト: gruntjs-updater/grunt-jison
	this.files.forEach(function(f) {


		var src = f.src.shift();
		var dest = f.dest;

		if (!src) {
			grunt.warn('Missing src property.');
			return false;
		}

		if (!dest) {
			grunt.warn('Missing dest property');
			return false;
		}


		try {
      var data = file.read(src);
      var grammar = ebnfParser.parse(data);
      var lex = f.src.shift();
      if (lex) {
        var lexFile = file.read(lex);
        var lexOpts = lexParser.parse(lexFile);
        grammar.lex = lexOpts;
      }
      var parser = new jison.Generator(grammar, options);
      var js = parser.generate(options);
      file.write(dest, js);
      grunt.log.oklns("generate "+dest);
			return true;
		} catch (e) {
      grunt.warn(e);
			return false;
		}
    });
コード例 #6
0
	grunt.registerMultiTask('jison-processor', 'Parse files based on grammars, generate standalone parsers, or both.', function() {
		var options = this.options({
			name: 'parser',
			parser: 'lalr',
			type: 'commonjs'
		});
		
		
		var grammarSource = options.grammar;
		if (typeof grammarSource === 'string' && grunt.file.exists(grammarSource)) {
			grammarSource = grunt.file.read(grammarSource);
		}
		
		var grammar;
		if (typeof grammarSource === 'string') {
			try {
				grammar = ebnfParser.parse(grammarSource);
			} catch(err) {
				try {
					grammar = JSON.parse(grammarSource);
					if (grammar.ebnf) {
						grammar = ebnfParser.transform(grammar);
					}
				} catch(err) {
					grunt.fatal('Cannot parse options.grammar');
				}
			}
		} else if (typeof grammarSource === 'object') {
			if (grammarSource.ebnf) {
				grammar = ebnfParser.transform(grammarSource);
			} else {
				grammar = grammarSource;
			}
		} else {
			grunt.fatal('options.grammar required');
		}
		
		
		var lexerSource = options.lexer;
		if (typeof lexerSource === 'string' && grunt.file.exists(lexerSource)) {
			lexerSource = grunt.file.read(lexerSource);
		}
		
		if (typeof lexerSource === 'string') {
			try {
				grammar.lex = lexParser.parse(lexerSource);
			} catch(err) {
				try {
					grammar.lex = JSON.parse(lexerSource);
				} catch(err) {
					grunt.fatal('Cannot parse options.lexer');
				}
			}
		} else if (typeof lexerSource === 'object') {
            grammar.lex = lexerSource;
		}
        
		
		if (options.output) {
			var settings = {
				debug: options.debug,
				moduleName: options.name,
				moduleParser: options.parser,
				moduleType: options.type
			};
			
			var generator = new jison.Generator(grammar, settings);
			var parserSource = generator.generate(settings);
			grunt.file.write(options.output, parserSource);
		}
		
		
		var parser = new jison.Parser(grammar);
		
		function compile(src, dest) {
			src = grunt.file.read(src);
			var compiled = parser.parse(src);
			grunt.file.write(dest, compiled);
		}
		
		this.files.forEach(function(file) {
			if (file.src.length === 1) {
				compile(file.src[0], file.dest);
			} else if (file.src.length > 1) {
				file.src.forEach(function(src) {
					compile(src, path.join(file.dest, path.basename(src)));
				});
			} else {
				grunt.log.writeln(file.dest + ' has no sources, skipping');
			}
		});
	});
コード例 #7
0
ファイル: ebnf-parser.js プロジェクト: CarloAl/thesis
var parseLex = function (text) {
    return jisonlex.parse(text.replace(/(?:^%lex)|(?:\/lex$)/g, ''));
};
コード例 #8
0
ファイル: build.js プロジェクト: wesavetheworld/php-parser
fs.writeFileSync(
  GLAYZZLE_PATH + '../tokens.js',
  fs.readFileSync(GLAYZZLE_PATH + 'license.txt', "utf8")
  +'\n// exports token index\n'
  +'module.exports = {\n'
  +'  values: {\n'
  +'    '+tokenValues.join(',\n    ')
  +'\n  },\n'
  +'  names: {\n'
  +'    '+tokenNames.join(',\n    ')
  +'\n  }\n'
  +'};'
);

// parse grammar
var lexerGrammar = LexerParser.parse(lexer);
if (!lexerGrammar.options) lexerGrammar.options = {};  
lexerGrammar.options.moduleName = 'lexer'; // used exported variable
lexerGrammar.options.moduleType = 'js';


// Generating the lexer
console.log('...Building lexer.js');
fs.writeFileSync(
  GLAYZZLE_PATH + '../lexer.js',
  fs.readFileSync(GLAYZZLE_PATH + 'license.txt', "utf8")
  +'\nvar ' + tokenVars.join(',\n  ') + ';\n'
  + fs.readFileSync(GLAYZZLE_PATH + 'header.js') + '\n'
  + Lexer.generate(lexerGrammar) 
  + '\n\n' + fs.readFileSync(GLAYZZLE_PATH + 'footer.js') 
);