Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow attaching metadata to any squim type

<thing> : {...}

attaches the key value pairs in {...} to <thing>
  • Loading branch information...
commit 29ff591800ce3ae06936ac831e9376a36a034299 1 parent b612ef7
Mariano Guerra authored
30 src/squim.parser.jison
View
@@ -16,11 +16,12 @@
"#inert" return 'INERT'
"#ignore" return 'IGNORE'
"\"".*?"\"" return 'STRING'
-[A-Za-z0-9!\$%&\*\+\-\./:<=>\?@\^\_~]+ return 'SYMBOL'
+[A-Za-z0-9!\$%&\*\+\-/<=>\?@\^\_~]+ return 'SYMBOL'
"(" return '('
")" return ')'
"{" return '{'
"}" return '}'
+":" return ':'
<<EOF>> return 'EOF'
. return 'INVALID'
@@ -53,8 +54,15 @@ objPairs: SYMBOL e
}
;
-e
- : '(' ')'
+obj :
+ '{' '}'
+ {$$ = new Types.Obj();}
+ | '{' objPairs '}'
+ {$$ = new Types.Obj($2);}
+ ;
+
+list :
+ '(' ')'
{$$ = Types.nil;}
| '(' listItems DOT e ')'
{
@@ -70,10 +78,12 @@ e
}
| '(' listItems ')'
{$$ = $2;}
- | '{' '}'
- {$$ = new Types.Obj();}
- | '{' objPairs '}'
- {$$ = new Types.Obj($2);}
+ ;
+
+
+literal :
+ list
+ | obj
| INTEGER
{$$ = new Types.Int(parseInt(yytext, 10));}
| DECIMAL
@@ -115,4 +125,10 @@ e
{$$ = Types.ignore;}
;
+e :
+ literal ':' obj
+ { $1.meta = $3.attrs; return $1; }
+ | literal
+ ;
+
80 src/squim.parser.js
View
@@ -1,9 +1,9 @@
define(['squim.types'], function (Types){
var parser = {trace: function trace() { },
yy: {},
-symbols_: {"error":2,"expressions":3,"e":4,"EOF":5,"listItems":6,"objPairs":7,"SYMBOL":8,"(":9,")":10,"DOT":11,"{":12,"}":13,"INTEGER":14,"DECIMAL":15,"BASEINTEGER":16,"STRING":17,"TRUE":18,"FALSE":19,"INERT":20,"IGNORE":21,"$accept":0,"$end":1},
-terminals_: {2:"error",5:"EOF",8:"SYMBOL",9:"(",10:")",11:"DOT",12:"{",13:"}",14:"INTEGER",15:"DECIMAL",16:"BASEINTEGER",17:"STRING",18:"TRUE",19:"FALSE",20:"INERT",21:"IGNORE"},
-productions_: [0,[3,2],[6,1],[6,2],[7,2],[7,3],[4,2],[4,5],[4,3],[4,2],[4,3],[4,1],[4,1],[4,1],[4,1],[4,1],[4,1],[4,1],[4,1],[4,1]],
+symbols_: {"error":2,"expressions":3,"e":4,"EOF":5,"listItems":6,"objPairs":7,"SYMBOL":8,"obj":9,"{":10,"}":11,"list":12,"(":13,")":14,"DOT":15,"literal":16,"INTEGER":17,"DECIMAL":18,"BASEINTEGER":19,"STRING":20,"TRUE":21,"FALSE":22,"INERT":23,"IGNORE":24,":":25,"$accept":0,"$end":1},
+terminals_: {2:"error",5:"EOF",8:"SYMBOL",10:"{",11:"}",13:"(",14:")",15:"DOT",17:"INTEGER",18:"DECIMAL",19:"BASEINTEGER",20:"STRING",21:"TRUE",22:"FALSE",23:"INERT",24:"IGNORE",25:":"},
+productions_: [0,[3,2],[6,1],[6,2],[7,2],[7,3],[9,2],[9,3],[12,2],[12,5],[12,3],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[16,1],[4,3],[4,1]],
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
var $0 = $$.length - 1;
@@ -24,9 +24,13 @@ case 5:
this.$ = $$[$0];
break;
-case 6:this.$ = Types.nil;
+case 6:this.$ = new Types.Obj();
break;
-case 7:
+case 7:this.$ = new Types.Obj($$[$0-1]);
+break;
+case 8:this.$ = Types.nil;
+break;
+case 9:
var pair = $$[$0-3];
while (pair.right !== Types.Pair.nil) {
@@ -38,17 +42,13 @@ case 7:
this.$ = $$[$0-3];
break;
-case 8:this.$ = $$[$0-1];
-break;
-case 9:this.$ = new Types.Obj();
+case 10:this.$ = $$[$0-1];
break;
-case 10:this.$ = new Types.Obj($$[$0-1]);
+case 13:this.$ = new Types.Int(parseInt(yytext, 10));
break;
-case 11:this.$ = new Types.Int(parseInt(yytext, 10));
+case 14:this.$ = new Types.Float(parseFloat(yytext));
break;
-case 12:this.$ = new Types.Float(parseFloat(yytext));
-break;
-case 13:
+case 15:
var str, sign = yytext.charAt(0), baseChar, base;
if (sign === "+" || sign === "-") {
@@ -71,22 +71,24 @@ case 13:
this.$ = new Types.Int(parseInt(sign + str, base));
break;
-case 14:this.$ = new Types.Str(yytext.slice(1, yytext.length - 1));
+case 16:this.$ = new Types.Str(yytext.slice(1, yytext.length - 1));
+break;
+case 17:this.$ = new Types.Symbol(yytext);
break;
-case 15:this.$ = new Types.Symbol(yytext);
+case 18:this.$ = Types.t;
break;
-case 16:this.$ = Types.t;
+case 19:this.$ = Types.f;
break;
-case 17:this.$ = Types.f;
+case 20:this.$ = Types.inert;
break;
-case 18:this.$ = Types.inert;
+case 21:this.$ = Types.ignore;
break;
-case 19:this.$ = Types.ignore;
+case 22: $$[$0-2].meta = $$[$0].attrs; return $$[$0-2];
break;
}
},
-table: [{3:1,4:2,8:[1,9],9:[1,3],12:[1,4],14:[1,5],15:[1,6],16:[1,7],17:[1,8],18:[1,10],19:[1,11],20:[1,12],21:[1,13]},{1:[3]},{5:[1,14]},{4:17,6:16,8:[1,9],9:[1,3],10:[1,15],12:[1,4],14:[1,5],15:[1,6],16:[1,7],17:[1,8],18:[1,10],19:[1,11],20:[1,12],21:[1,13]},{7:19,8:[1,20],13:[1,18]},{5:[2,11],8:[2,11],9:[2,11],10:[2,11],11:[2,11],12:[2,11],13:[2,11],14:[2,11],15:[2,11],16:[2,11],17:[2,11],18:[2,11],19:[2,11],20:[2,11],21:[2,11]},{5:[2,12],8:[2,12],9:[2,12],10:[2,12],11:[2,12],12:[2,12],13:[2,12],14:[2,12],15:[2,12],16:[2,12],17:[2,12],18:[2,12],19:[2,12],20:[2,12],21:[2,12]},{5:[2,13],8:[2,13],9:[2,13],10:[2,13],11:[2,13],12:[2,13],13:[2,13],14:[2,13],15:[2,13],16:[2,13],17:[2,13],18:[2,13],19:[2,13],20:[2,13],21:[2,13]},{5:[2,14],8:[2,14],9:[2,14],10:[2,14],11:[2,14],12:[2,14],13:[2,14],14:[2,14],15:[2,14],16:[2,14],17:[2,14],18:[2,14],19:[2,14],20:[2,14],21:[2,14]},{5:[2,15],8:[2,15],9:[2,15],10:[2,15],11:[2,15],12:[2,15],13:[2,15],14:[2,15],15:[2,15],16:[2,15],17:[2,15],18:[2,15],19:[2,15],20:[2,15],21:[2,15]},{5:[2,16],8:[2,16],9:[2,16],10:[2,16],11:[2,16],12:[2,16],13:[2,16],14:[2,16],15:[2,16],16:[2,16],17:[2,16],18:[2,16],19:[2,16],20:[2,16],21:[2,16]},{5:[2,17],8:[2,17],9:[2,17],10:[2,17],11:[2,17],12:[2,17],13:[2,17],14:[2,17],15:[2,17],16:[2,17],17:[2,17],18:[2,17],19:[2,17],20:[2,17],21:[2,17]},{5:[2,18],8:[2,18],9:[2,18],10:[2,18],11:[2,18],12:[2,18],13:[2,18],14:[2,18],15:[2,18],16:[2,18],17:[2,18],18:[2,18],19:[2,18],20:[2,18],21:[2,18]},{5:[2,19],8:[2,19],9:[2,19],10:[2,19],11:[2,19],12:[2,19],13:[2,19],14:[2,19],15:[2,19],16:[2,19],17:[2,19],18:[2,19],19:[2,19],20:[2,19],21:[2,19]},{1:[2,1]},{5:[2,6],8:[2,6],9:[2,6],10:[2,6],11:[2,6],12:[2,6],13:[2,6],14:[2,6],15:[2,6],16:[2,6],17:[2,6],18:[2,6],19:[2,6],20:[2,6],21:[2,6]},{10:[1,22],11:[1,21]},{4:17,6:23,8:[1,9],9:[1,3],10:[2,2],11:[2,2],12:[1,4],14:[1,5],15:[1,6],16:[1,7],17:[1,8],18:[1,10],19:[1,11],20:[1,12],21:[1,13]},{5:[2,9],8:[2,9],9:[2,9],10:[2,9],11:[2,9],12:[2,9],13:[2,9],14:[2,9],15:[2,9],16:[2,9],17:[2,9],18:[2,9],19:[2,9],20:[2,9],21:[2,9]},{13:[1,24]},{4:25,8:[1,9],9:[1,3],12:[1,4],14:[1,5],15:[1,6],16:[1,7],17:[1,8],18:[1,10],19:[1,11],20:[1,12],21:[1,13]},{4:26,8:[1,9],9:[1,3],12:[1,4],14:[1,5],15:[1,6],16:[1,7],17:[1,8],18:[1,10],19:[1,11],20:[1,12],21:[1,13]},{5:[2,8],8:[2,8],9:[2,8],10:[2,8],11:[2,8],12:[2,8],13:[2,8],14:[2,8],15:[2,8],16:[2,8],17:[2,8],18:[2,8],19:[2,8],20:[2,8],21:[2,8]},{10:[2,3],11:[2,3]},{5:[2,10],8:[2,10],9:[2,10],10:[2,10],11:[2,10],12:[2,10],13:[2,10],14:[2,10],15:[2,10],16:[2,10],17:[2,10],18:[2,10],19:[2,10],20:[2,10],21:[2,10]},{7:27,8:[1,20],13:[2,4]},{10:[1,28]},{13:[2,5]},{5:[2,7],8:[2,7],9:[2,7],10:[2,7],11:[2,7],12:[2,7],13:[2,7],14:[2,7],15:[2,7],16:[2,7],17:[2,7],18:[2,7],19:[2,7],20:[2,7],21:[2,7]}],
-defaultActions: {14:[2,1],27:[2,5]},
+table: [{3:1,4:2,8:[1,10],9:5,10:[1,16],12:4,13:[1,15],16:3,17:[1,6],18:[1,7],19:[1,8],20:[1,9],21:[1,11],22:[1,12],23:[1,13],24:[1,14]},{1:[3]},{5:[1,17]},{5:[2,23],8:[2,23],10:[2,23],11:[2,23],13:[2,23],14:[2,23],15:[2,23],17:[2,23],18:[2,23],19:[2,23],20:[2,23],21:[2,23],22:[2,23],23:[2,23],24:[2,23],25:[1,18]},{5:[2,11],8:[2,11],10:[2,11],11:[2,11],13:[2,11],14:[2,11],15:[2,11],17:[2,11],18:[2,11],19:[2,11],20:[2,11],21:[2,11],22:[2,11],23:[2,11],24:[2,11],25:[2,11]},{5:[2,12],8:[2,12],10:[2,12],11:[2,12],13:[2,12],14:[2,12],15:[2,12],17:[2,12],18:[2,12],19:[2,12],20:[2,12],21:[2,12],22:[2,12],23:[2,12],24:[2,12],25:[2,12]},{5:[2,13],8:[2,13],10:[2,13],11:[2,13],13:[2,13],14:[2,13],15:[2,13],17:[2,13],18:[2,13],19:[2,13],20:[2,13],21:[2,13],22:[2,13],23:[2,13],24:[2,13],25:[2,13]},{5:[2,14],8:[2,14],10:[2,14],11:[2,14],13:[2,14],14:[2,14],15:[2,14],17:[2,14],18:[2,14],19:[2,14],20:[2,14],21:[2,14],22:[2,14],23:[2,14],24:[2,14],25:[2,14]},{5:[2,15],8:[2,15],10:[2,15],11:[2,15],13:[2,15],14:[2,15],15:[2,15],17:[2,15],18:[2,15],19:[2,15],20:[2,15],21:[2,15],22:[2,15],23:[2,15],24:[2,15],25:[2,15]},{5:[2,16],8:[2,16],10:[2,16],11:[2,16],13:[2,16],14:[2,16],15:[2,16],17:[2,16],18:[2,16],19:[2,16],20:[2,16],21:[2,16],22:[2,16],23:[2,16],24:[2,16],25:[2,16]},{5:[2,17],8:[2,17],10:[2,17],11:[2,17],13:[2,17],14:[2,17],15:[2,17],17:[2,17],18:[2,17],19:[2,17],20:[2,17],21:[2,17],22:[2,17],23:[2,17],24:[2,17],25:[2,17]},{5:[2,18],8:[2,18],10:[2,18],11:[2,18],13:[2,18],14:[2,18],15:[2,18],17:[2,18],18:[2,18],19:[2,18],20:[2,18],21:[2,18],22:[2,18],23:[2,18],24:[2,18],25:[2,18]},{5:[2,19],8:[2,19],10:[2,19],11:[2,19],13:[2,19],14:[2,19],15:[2,19],17:[2,19],18:[2,19],19:[2,19],20:[2,19],21:[2,19],22:[2,19],23:[2,19],24:[2,19],25:[2,19]},{5:[2,20],8:[2,20],10:[2,20],11:[2,20],13:[2,20],14:[2,20],15:[2,20],17:[2,20],18:[2,20],19:[2,20],20:[2,20],21:[2,20],22:[2,20],23:[2,20],24:[2,20],25:[2,20]},{5:[2,21],8:[2,21],10:[2,21],11:[2,21],13:[2,21],14:[2,21],15:[2,21],17:[2,21],18:[2,21],19:[2,21],20:[2,21],21:[2,21],22:[2,21],23:[2,21],24:[2,21],25:[2,21]},{4:21,6:20,8:[1,10],9:5,10:[1,16],12:4,13:[1,15],14:[1,19],16:3,17:[1,6],18:[1,7],19:[1,8],20:[1,9],21:[1,11],22:[1,12],23:[1,13],24:[1,14]},{7:23,8:[1,24],11:[1,22]},{1:[2,1]},{9:25,10:[1,16]},{5:[2,8],8:[2,8],10:[2,8],11:[2,8],13:[2,8],14:[2,8],15:[2,8],17:[2,8],18:[2,8],19:[2,8],20:[2,8],21:[2,8],22:[2,8],23:[2,8],24:[2,8],25:[2,8]},{14:[1,27],15:[1,26]},{4:21,6:28,8:[1,10],9:5,10:[1,16],12:4,13:[1,15],14:[2,2],15:[2,2],16:3,17:[1,6],18:[1,7],19:[1,8],20:[1,9],21:[1,11],22:[1,12],23:[1,13],24:[1,14]},{5:[2,6],8:[2,6],10:[2,6],11:[2,6],13:[2,6],14:[2,6],15:[2,6],17:[2,6],18:[2,6],19:[2,6],20:[2,6],21:[2,6],22:[2,6],23:[2,6],24:[2,6],25:[2,6]},{11:[1,29]},{4:30,8:[1,10],9:5,10:[1,16],12:4,13:[1,15],16:3,17:[1,6],18:[1,7],19:[1,8],20:[1,9],21:[1,11],22:[1,12],23:[1,13],24:[1,14]},{5:[2,22],8:[2,22],10:[2,22],11:[2,22],13:[2,22],14:[2,22],15:[2,22],17:[2,22],18:[2,22],19:[2,22],20:[2,22],21:[2,22],22:[2,22],23:[2,22],24:[2,22]},{4:31,8:[1,10],9:5,10:[1,16],12:4,13:[1,15],16:3,17:[1,6],18:[1,7],19:[1,8],20:[1,9],21:[1,11],22:[1,12],23:[1,13],24:[1,14]},{5:[2,10],8:[2,10],10:[2,10],11:[2,10],13:[2,10],14:[2,10],15:[2,10],17:[2,10],18:[2,10],19:[2,10],20:[2,10],21:[2,10],22:[2,10],23:[2,10],24:[2,10],25:[2,10]},{14:[2,3],15:[2,3]},{5:[2,7],8:[2,7],10:[2,7],11:[2,7],13:[2,7],14:[2,7],15:[2,7],17:[2,7],18:[2,7],19:[2,7],20:[2,7],21:[2,7],22:[2,7],23:[2,7],24:[2,7],25:[2,7]},{7:32,8:[1,24],11:[2,4]},{14:[1,33]},{11:[2,5]},{5:[2,9],8:[2,9],10:[2,9],11:[2,9],13:[2,9],14:[2,9],15:[2,9],17:[2,9],18:[2,9],19:[2,9],20:[2,9],21:[2,9],22:[2,9],23:[2,9],24:[2,9],25:[2,9]}],
+defaultActions: {17:[2,1],32:[2,5]},
parseError: function parseError(str, hash) {
throw new Error(str);
},
@@ -415,42 +417,44 @@ case 1:/* ignore */
break;
case 2:/* skip whitespace */
break;
-case 3:return 11
+case 3:return 15
break;
-case 4:return 15
+case 4:return 18
break;
-case 5:return 14
+case 5:return 17
break;
-case 6:return 16
+case 6:return 19
break;
-case 7:return 18
+case 7:return 21
break;
-case 8:return 19
+case 8:return 22
break;
-case 9:return 20
+case 9:return 23
break;
-case 10:return 21
+case 10:return 24
break;
-case 11:return 17
+case 11:return 20
break;
case 12:return 8
break;
-case 13:return 9
+case 13:return 13
+break;
+case 14:return 14
break;
-case 14:return 10
+case 15:return 10
break;
-case 15:return 12
+case 16:return 11
break;
-case 16:return 13
+case 17:return 25
break;
-case 17:return 5
+case 18:return 5
break;
-case 18:return 'INVALID'
+case 19:return 'INVALID'
break;
}
};
-lexer.rules = [/^;.*\n+/,/^;.*$/,/^\s+/,/^\./,/^[\-\+]?[0-9]+\.[0-9]+\b/,/^[\-\+]?[0-9]+\b/,/^[\-\+]?#[bodx][0-9a-zA-Z]+\b/,/^#t\b/,/^#f\b/,/^#inert\b/,/^#ignore\b/,/^".*?"/,/^[A-Za-z0-9!\$%&\*\+\-\./:<=>\?@\^\_~]+/,/^\(/,/^\)/,/^\{/,/^\}/,/^$/,/^./];
-lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],"inclusive":true}};
+lexer.rules = [/^;.*\n+/,/^;.*$/,/^\s+/,/^\./,/^[\-\+]?[0-9]+\.[0-9]+\b/,/^[\-\+]?[0-9]+\b/,/^[\-\+]?#[bodx][0-9a-zA-Z]+\b/,/^#t\b/,/^#f\b/,/^#inert\b/,/^#ignore\b/,/^".*?"/,/^[A-Za-z0-9!\$%&\*\+\-/<=>\?@\^\_~]+/,/^\(/,/^\)/,/^\{/,/^\}/,/^:/,/^$/,/^./];
+lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19],"inclusive":true}};
return lexer;})()
parser.lexer = lexer;
return parser;
26 test/squim.parser.test.js
View
@@ -53,7 +53,7 @@
}
check("(foo)", "foo");
- check("(foo.bar:baz/argh)", "foo.bar:baz/argh");
+ check("(foo-bar!baz/argh)", "foo-bar!baz/argh");
check("($set!)", "$set!");
check("($long-dashed-name-42!)", "$long-dashed-name-42!");
});
@@ -134,7 +134,6 @@
for (key in expected) {
value = expected[key];
- console.log(exp, exp.attrs);
Q.deepEqual(exp.attrs[key].value, value);
}
}
@@ -151,6 +150,29 @@
}
});
+ Q.test("attaches metadata to objects", function () {
+ function check(expr, expected) {
+ var exp = Parser.parse(expr), key, value;
+ console.log(exp, exp.meta);
+
+ for (key in expected) {
+ value = expected[key];
+ Q.deepEqual(exp.meta[key].value, value);
+ }
+ }
+
+ check("1 : {}", {});
+ check("foo : {a 1}", {a: 1});
+ check('"lala" : {a 1 b #f}', {a: 1, b: false});
+ check('(foo) : {a 1 b #f c "hello"}', {a: 1, b: false, c: "hello"});
+
+ try {
+ Parser.parse("{a}");
+ Q.ok(false, "expected the test to fail");
+ } catch (error) {
+ }
+ });
+
Q.test("parses simple expressions", function () {
function check(expr, expectedValue) {
var exp = Parser.parse(expr);
Please sign in to comment.
Something went wrong with that request. Please try again.