123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- var assert = require("assert.js").ok;
- var Errors = require("errors.js");
- var Lexer = require("lexer.js");
- exports.and = function(/*...*/){
- var args = arguments;
- assert(args.length >= 2);
- return function(stream, context){
- var savePos = stream.pos();
- for(var i = 0; i < args.length; ++i){
- if (i != 0)
- Lexer.skipSpaces(stream, context);
-
- var p = args[i];
- if (typeof p == "string")
- p = Lexer.literal(p);
-
- if (!p(stream, context)){
- stream.setPos(savePos);
- return false;
- }
- }
- return true;
- }
- }
- exports.or = function(/*...*/){
- var args = arguments;
- assert(args.length >= 2);
- return function(stream, context){
- for(var i = 0; i < args.length; ++i){
- var p = args[i];
- if (typeof p == "string")
- p = Lexer.literal(p);
-
- var savePos = stream.pos();
- if (p(stream, context))
- return true;
- stream.setPos(savePos);
- }
- return false;
- }
- }
- exports.repeat = function(p){
- return function(stream, context){
- var savePos = stream.pos();
- while (!stream.eof() && p(stream, context)){
- Lexer.skipSpaces(stream, context);
- savePos = stream.pos();
- }
- stream.setPos(savePos);
- return true;
- }
- }
- exports.optional = function(p){
- assert(arguments.length == 1);
- return function(stream, context){
- var savePos = stream.pos();
- if ( !p(stream, context))
- stream.setPos(savePos);
- return true;
- }
- }
- exports.required = function(parser, error){
- if (typeof(parser) === "string")
- parser = Lexer.literal(parser);
- return function(stream, context){
- if (!parser(stream, context))
- throw new Errors.Error(error);
- return true;
- }
- }
- exports.context = function(parser, contextFactory){
- return function(stream, context){
- var context = new contextFactory(context);
- if (!parser(stream, context))
- return false;
- if (context.endParse)
- return context.endParse() !== false;
- return true;
- }
- }
- exports.emit = function(parser, action){
- assert(action);
- if (typeof(parser) === "string")
- parser = Lexer.literal(parser);
- return function(stream, context){
- if (!parser(stream, context))
- return false;
- action(context);
- return true;
- }
- }
|