Kaynağa Gözat

fix strings with spaces

Vladislav Folts 12 yıl önce
ebeveyn
işleme
0ccc4b8b14
5 değiştirilmiş dosya ile 30 ekleme ve 4 silme
  1. 3 2
      src/context.js
  2. 1 2
      src/grammar.js
  3. 24 0
      src/lexer.js
  4. 1 0
      test/expected/string.js
  5. 1 0
      test/input/string.ob

+ 3 - 2
src/context.js

@@ -111,9 +111,9 @@ function escapeString(s){
 exports.String = ChainedContext.extend({
 	init: function StringContext(context){
 		ChainedContext.prototype.init.bind(this)(context);
-		this.__result = "";
+		this.__result = undefined;
 	},
-	handleChar: function(c){this.__result += c;},
+	handleString: function(s){this.__result = s;},
 	toStr: function(s){return s;},
 	endParse: function(){
 		var s = this.toStr(this.__result);
@@ -127,6 +127,7 @@ exports.Char = exports.String.extend({
 		exports.String.prototype.init.bind(this)(context);
 		this.__result = "";
 	},
+	handleChar: function(c){this.__result += c;},
 	toStr: function(s){return String.fromCharCode(parseInt(s, 16));}
 });
 

+ 1 - 2
src/grammar.js

@@ -41,8 +41,7 @@ var real = context(and(digit, repeat(digit), point, repeat(digit), optional(scal
 
 var number = or(real, integer);
 
-var string = or(context(and("\"", repeat(character), required("\"", "unexpected end of string"))
-					  , Context.String)
+var string = or(context(Lexer.string, Context.String)
 			  , context(and(digit, repeat(hexDigit), "X"), Context.Char));
 
 var factor = context(

+ 24 - 0
src/lexer.js

@@ -34,6 +34,29 @@ exports.character = function(stream, context){
 	return true;
 }
 
+function string(stream, context){
+	var c = stream.char();
+	if (c != '"')
+		return false;
+
+	var result = "";
+	var parsed = false;
+	stream.read(function(c){
+		if (c == '"'){
+			parsed = true;
+			return false;
+		}
+		result += c;
+		return true;
+	});
+	if (!parsed)
+		throw new Errors.Error("unexpected end of string");
+
+	stream.next(1);
+	context.handleString(result);
+	return true;
+}
+
 function isLetter(c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');}
 
 exports.ident = function(stream, context){
@@ -102,3 +125,4 @@ exports.literal = function(s){
 	}
 }	
 
+exports.string = string;

+ 1 - 0
test/expected/string.js

@@ -1,4 +1,5 @@
 var m = function (){
 var s1 = "\"";
 var s2 = "ABC";
+var s3 = "with space";
 }();

+ 1 - 0
test/input/string.ob

@@ -3,5 +3,6 @@ MODULE m;
 CONST
 	s1 = 22X;
 	s2 = "ABC";
+	s3 = "with space";
 
 END m.