Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 3f129f4256
...
compare: 3865afc608
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
119 spec/parser.spec.js
@@ -1,7 +1,7 @@
var _ = require('Underscore');
var multimethod = require('multimethod');
-var parser = require('../src/parser').Parser;
+var p = require('../src/parser').Parser;
var utils = require("../src/utils").Utils;
var checkAst = multimethod()
@@ -25,7 +25,7 @@ var checkAst = multimethod()
describe('parser', function() {
describe('assignment to scalar', function(){
it('should assign an identifier', function() {
- checkAst(parser.parse("isla is age"),
+ checkAst(p.parse("isla is age"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
@@ -35,7 +35,7 @@ describe('parser', function() {
});
it('should assign a string in single quotes', function() {
- checkAst(parser.parse("isla is 'cool'"),
+ checkAst(p.parse("isla is 'cool'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
@@ -44,7 +44,7 @@ describe('parser', function() {
});
it('should assign a string in double quotes', function() {
- checkAst(parser.parse("isla is \"cool\""),
+ checkAst(p.parse("isla is \"cool\""),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
@@ -54,14 +54,14 @@ describe('parser', function() {
it('should not parse assignment of int', function() {
expect(function(){
- parser.parse("age is 1");
+ p.parse("age is 1");
}).toThrow();
});
});
describe('assignment to object attribute', function(){
it('should allow assignment', function() {
- checkAst(parser.parse("isla age is '1'"),
+ checkAst(p.parse("isla age is '1'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{object:
@@ -74,7 +74,7 @@ describe('parser', function() {
describe('type assignment', function(){
it('should allow assignment to scalar', function() {
- checkAst(parser.parse("mary is a girl"),
+ checkAst(p.parse("mary is a girl"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{scalar: [{identifier: ["mary"]}]}]},
@@ -83,7 +83,7 @@ describe('parser', function() {
});
it('should allow assignment to an object attribute', function() {
- checkAst(parser.parse("mary friend is a girl"),
+ checkAst(p.parse("mary friend is a girl"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{object:
@@ -96,7 +96,7 @@ describe('parser', function() {
describe('blocks', function(){
it('should allow a two expression block', function() {
- checkAst(parser.parse("isla is '1'\nmary is '2'"),
+ checkAst(p.parse("isla is '1'\nmary is '2'"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["isla"]}]}]},
@@ -110,7 +110,7 @@ describe('parser', function() {
});
it('should allow a three expression block', function() {
- checkAst(parser.parse("name is 'Isla'\nwrite 'la'\nwrite name"),
+ checkAst(p.parse("name is 'Isla'\nwrite 'la'\nwrite name"),
{root: [{block: [{expression:
[{value_assignment:
[{assignee: [{scalar: [{identifier: ["name"]}]}]},
@@ -131,36 +131,61 @@ describe('parser', function() {
describe('whitespace', function() {
it('should allow whitespace at end of expressions', function() {
- expect(parser.parse("x is y ").tag).toEqual("root");
- expect(parser.parse("write x ").tag).toEqual("root");
- expect(parser.parse("add x to y ").tag).toEqual("root");
- expect(parser.parse("x is a y ").tag).toEqual("root");
+ expect(p.parse("x is y ").tag).toEqual("root");
+ expect(p.parse("write x ").tag).toEqual("root");
+ expect(p.parse("add x to y ").tag).toEqual("root");
+ expect(p.parse("x is a y ").tag).toEqual("root");
});
it('should allow whitespace at beginning of expressions', function() {
- expect(parser.parse(" x is y").tag).toEqual("root");
- expect(parser.parse(" write x").tag).toEqual("root");
- expect(parser.parse(" add x to y").tag).toEqual("root");
- expect(parser.parse(" x is a y").tag).toEqual("root");
+ expect(p.parse(" x is y").tag).toEqual("root");
+ expect(p.parse(" write x").tag).toEqual("root");
+ expect(p.parse(" add x to y").tag).toEqual("root");
+ expect(p.parse(" x is a y").tag).toEqual("root");
});
it('should allow whitespace at in middle of expressions', function() {
- expect(parser.parse("x is y").tag).toEqual("root");
- expect(parser.parse("write x").tag).toEqual("root");
- expect(parser.parse("add x to y").tag).toEqual("root");
- expect(parser.parse("x is a y").tag).toEqual("root");
+ expect(p.parse("x is y").tag).toEqual("root");
+ expect(p.parse("write x").tag).toEqual("root");
+ expect(p.parse("add x to y").tag).toEqual("root");
+ expect(p.parse("x is a y").tag).toEqual("root");
+ });
+ });
+
+ describe('index of node in source', function() {
+ var expr = function(ast) {
+ return p.extract(ast, "root", 0, "block", 0, "expression", 0);
+ };
+
+ it('should set index of 0 for beginning of expression', function() {
+ expect(expr(p.parse("x is y")).index).toEqual(0);
+ });
+
+ it('should set index of 0 for assignee at beginning of expression', function() {
+ var e = expr(p.parse("x is y"));
+ expect(p.extract(e, "value_assignment", 0).index).toEqual(0);
+ });
+
+ it('should set index of 2 for is in expression', function() {
+ var e = expr(p.parse("x is y"));
+ expect(p.extract(e, "value_assignment", 1).index).toEqual(2);
+ });
+
+ it('should set correct col when whitespace in code', function() {
+ var e = expr(p.parse("x is y"));
+ expect(p.extract(e, "value_assignment", 1).index).toEqual(12);
});
});
describe('invocation', function() {
it('should not parse invocation with int param', function() {
expect(function(){
- parser.parse("write 1");
+ p.parse("write 1");
}).toThrow();
});
it('should allow invocation with scalar variable', function() {
- checkAst(parser.parse("write isla"),
+ checkAst(p.parse("write isla"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
@@ -169,7 +194,7 @@ describe('parser', function() {
});
it('should allow invocation with scalar literal', function() {
- checkAst(parser.parse("write 'isla'"),
+ checkAst(p.parse("write 'isla'"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
@@ -177,7 +202,7 @@ describe('parser', function() {
});
it('should allow invocation with object attribute', function() {
- checkAst(parser.parse("write isla age"),
+ checkAst(p.parse("write isla age"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
@@ -187,7 +212,7 @@ describe('parser', function() {
});
it('should not show string regression', function() {
- checkAst(parser.parse("write 'My name Isla'"),
+ checkAst(p.parse("write 'My name Isla'"),
{root: [{block: [{expression:
[{invocation:
[{identifier: ["write"]},
@@ -198,7 +223,7 @@ describe('parser', function() {
describe('lists', function() {
it('should allow list instantiation', function() {
- checkAst(parser.parse("items is a list"),
+ checkAst(p.parse("items is a list"),
{root: [{block: [{expression:
[{type_assignment:
[{assignee: [{scalar: [{identifier: ["items"]}]}]},
@@ -209,12 +234,12 @@ describe('parser', function() {
it('should not allow addition of int to list', function() {
expect(function(){
- parser.parse("add 1 to list");
+ p.parse("add 1 to list");
}).toThrow();
});
it('should allow addition of item to list', function() {
- checkAst(parser.parse("add sword to items"),
+ checkAst(p.parse("add sword to items"),
{root: [{block: [{expression:
[{list_assignment:
[{list_operation: [{add: ["add"]}]},
@@ -228,7 +253,7 @@ describe('parser', function() {
describe('syntax annotation', function() {
var astExpression = function(code) {
- return parser.extract(parser.parse(code),
+ return p.extract(p.parse(code),
"root", 0, "block", 0, "expression", 0).c;
};
@@ -287,12 +312,12 @@ describe('parser', function() {
describe('variables', function() {
describe('assignments', function() {
it('should annotate in scalar assignment', function() {
- expect(parser.extract(astExpression("x is 'y'"),
+ expect(p.extract(astExpression("x is 'y'"),
0, "assignee", 0).syntax).toEqual("variable");
});
it('should annotate in object assignment', function() {
- expect(parser.extract(astExpression("x y is 'z'"),
+ expect(p.extract(astExpression("x y is 'z'"),
0, "assignee", 0, "object", 0).syntax)
.toEqual("variable");
});
@@ -300,12 +325,12 @@ describe('parser', function() {
describe('type instantiations', function() {
it('should annotate in scalar type instantiation', function() {
- expect(parser.extract(astExpression("x is a y"),
+ expect(p.extract(astExpression("x is a y"),
0, "assignee", 0).syntax).toEqual("variable");
});
it('should annotate in an attribute type instantiation', function() {
- expect(parser.extract(astExpression("x y is a z"),
+ expect(p.extract(astExpression("x y is a z"),
0, "assignee", 0, "object", 0).syntax)
.toEqual("variable");
});
@@ -313,26 +338,26 @@ describe('parser', function() {
describe('list operations', function() {
it('should annotate item in a list operation', function() {
- expect(parser.extract(astExpression("add x to y"),
+ expect(p.extract(astExpression("add x to y"),
1, "value", 0, "variable", 0).syntax)
.toEqual("variable");
});
it('should annotate list in a list operation', function() {
- expect(parser.extract(astExpression("add x to y"),
+ expect(p.extract(astExpression("add x to y"),
3, "assignee", 0).syntax).toEqual("variable");
});
});
describe('invocations', function() {
it('should annotate scalar param', function() {
- expect(parser.extract(astExpression("write x"),
+ expect(p.extract(astExpression("write x"),
1, "value", 0, "variable", 0).syntax)
.toEqual("variable");
});
it('should annotate in object param', function() {
- expect(parser.extract(astExpression("write x y"),
+ expect(p.extract(astExpression("write x y"),
1, "value", 0, "variable", 0, "object", 0).syntax)
.toEqual("variable");
});
@@ -342,7 +367,7 @@ describe('parser', function() {
describe('function identifiers', function() {
describe('invocations', function() {
it('should annotate in invocation', function() {
- expect(parser.extract(astExpression("write x"),
+ expect(p.extract(astExpression("write x"),
0).syntax).toEqual("function");
});
@@ -352,21 +377,21 @@ describe('parser', function() {
describe('literals', function() {
describe('assignments', function() {
it('should annotate in assignment', function() {
- expect(parser.extract(astExpression("x is 'y'"),
+ expect(p.extract(astExpression("x is 'y'"),
2, "value", 0).syntax).toEqual("literal");
});
});
describe('list operations', function() {
it('should annotate item in a list operation', function() {
- expect(parser.extract(astExpression("add 'x' to y"),
+ expect(p.extract(astExpression("add 'x' to y"),
1, "value", 0).syntax).toEqual("literal");
});
});
describe('invocations', function() {
it('should annotate scalar param', function() {
- expect(parser.extract(astExpression("write 'x'"),
+ expect(p.extract(astExpression("write 'x'"),
1, "value", 0).syntax).toEqual("literal");
});
});
@@ -375,7 +400,7 @@ describe('parser', function() {
describe('attributes', function() {
describe('assignments', function() {
it('should annotate in object assignment', function() {
- expect(parser.extract(astExpression("x y is 'z'"),
+ expect(p.extract(astExpression("x y is 'z'"),
0, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
@@ -383,7 +408,7 @@ describe('parser', function() {
describe('type instantiations', function() {
it('should annotate in an attribute type instantiation', function() {
- expect(parser.extract(astExpression("x y is a z"),
+ expect(p.extract(astExpression("x y is a z"),
0, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
@@ -391,7 +416,7 @@ describe('parser', function() {
describe('list operations', function() {
it('should annotate is a list op on a obj attribute', function() {
- expect(parser.extract(astExpression("add x to y z"),
+ expect(p.extract(astExpression("add x to y z"),
3, "assignee", 0, "object", 1).syntax)
.toEqual("attribute");
});
@@ -399,7 +424,7 @@ describe('parser', function() {
describe('invocations', function() {
it('should annotate in object param', function() {
- expect(parser.extract(astExpression("write x y"),
+ expect(p.extract(astExpression("write x y"),
1, "value", 0, "variable", 0, "object", 1).syntax)
.toEqual("attribute");
});
View
66 src/grammar.js
@@ -3,8 +3,8 @@
var grammarArr = [
"{",
- " var nnode = function(tag, content, syntax, code) {",
- " var node = { tag: tag, c: content };",
+ " var nnode = function(tag, content, column, syntax, code) {",
+ " var node = { tag: tag, c: content, index: column - 1};",
" if(syntax !== undefined) {",
" node.syntax = syntax;",
" }",
@@ -23,83 +23,83 @@
"}",
"start",
- " = all:block { return nnode('root', [all]); }",
+ " = all:block { return nnode('root', [all], column); }",
"block",
- " = _* all:expression+ { return nnode('block', all); }",
+ " = _* all:expression+ { return nnode('block', all, column); }",
"expression",
- " = all:type_assignment { return nnode('expression', [all]); }",
- " / all:value_assignment { return nnode('expression', [all]); }",
- " / all:list_assignment { return nnode('expression', [all]); }",
- " / all:invocation { return nnode('expression', [all]); }",
+ " = all:type_assignment { return nnode('expression', [all], column); }",
+ " / all:value_assignment { return nnode('expression', [all], column); }",
+ " / all:list_assignment { return nnode('expression', [all], column); }",
+ " / all:invocation { return nnode('expression', [all], column); }",
"type_assignment",
" = a:assignee _ ia:is_a _ id:identifier _* nl",
- " { return nnode('type_assignment', [a, ia, syn(id, 'type')]); }",
+ " { return nnode('type_assignment', [a, ia, syn(id, 'type')], column); }",
"value_assignment",
" = a:assignee _ i:is _ v:value _* nl",
- " { return nnode('value_assignment', [a, i, v]); }",
+ " { return nnode('value_assignment', [a, i, v], column); }",
"list_assignment",
" = lo:list_operation _ va:value _ tf:to_from _ as:assignee _* nl",
- " { return nnode('list_assignment', [lo, va, tf, as]); }",
+ " { return nnode('list_assignment', [lo, va, tf, as], column); }",
"invocation",
" = a:identifier _ v:value _* nl",
- " { return nnode('invocation', [syn(a, 'function'), v]); }",
+ " { return nnode('invocation', [syn(a, 'function'), v], column); }",
"list_operation",
- " = all:add { return nnode('list_operation', [all]); }",
- " / all:take { return nnode('list_operation', [all]); }",
+ " = all:add { return nnode('list_operation', [all], column); }",
+ " / all:take { return nnode('list_operation', [all], column); }",
"assignee",
- " = all:object { return nnode('assignee', [all]); }",
- " / all:scalar { return nnode('assignee', [all]); }",
+ " = all:object { return nnode('assignee', [all], column); }",
+ " / all:scalar { return nnode('assignee', [all], column); }",
"value",
- " = all:literal { return nnode('value', [all]); }",
- " / all:variable { return nnode('value', [all]); }",
+ " = all:literal { return nnode('value', [all], column); }",
+ " / all:variable { return nnode('value', [all], column); }",
"literal",
- " = all:string { return nnode('literal', [all], 'literal'); }",
+ " = all:string { return nnode('literal', [all], column, 'literal'); }",
"identifier",
- " = !keyword init:[a-zA-Z] rest:identifier_char* { return nnode('identifier', [init + rest.join('')]); }",
+ " = !keyword init:[a-zA-Z] rest:identifier_char* { return nnode('identifier', [init + rest.join('')], column); }",
"variable",
- " = all:object { return nnode('variable', [all]); }",
- " / all:scalar { return nnode('variable', [all]); }",
+ " = all:object { return nnode('variable', [all], column); }",
+ " / all:scalar { return nnode('variable', [all], column); }",
"scalar",
- " = all:identifier { return nnode('scalar', [all], 'variable'); }",
+ " = all:identifier { return nnode('scalar', [all], column, 'variable'); }",
"object",
- " = id1:identifier _ id2:identifier { return nnode('object', [syn(id1, 'variable'), syn(id2, 'attribute')]); }",
+ " = id1:identifier _ id2:identifier { return nnode('object', [syn(id1, 'variable'), syn(id2, 'attribute')], column); }",
"is",
- " = all:'is' { return nnode('is', [all], 'keyword'); }",
+ " = all:'is' { return nnode('is', [all], column, 'keyword'); }",
"is_a",
- " = all:'is a' { return nnode('is_a', [all], 'keyword'); }",
+ " = all:'is a' { return nnode('is_a', [all], column, 'keyword'); }",
"add",
- " = all:'add' { return nnode('add', [all], 'keyword'); }",
+ " = all:'add' { return nnode('add', [all], column, 'keyword'); }",
"take",
- " = all:'take' { return nnode('take', [all], 'keyword'); }",
+ " = all:'take' { return nnode('take', [all], column, 'keyword'); }",
"to_from",
- " = all:to { return nnode('to_from', ['to from'], 'keyword', 'to'); }",
- " / all:from { return nnode('to_from', ['to from'], 'keyword', 'from'); }",
+ " = all:to { return nnode('to_from', ['to from'], column, 'keyword', 'to'); }",
+ " / all:from { return nnode('to_from', ['to from'], column, 'keyword', 'from'); }",
"string",
- " = \"'\" all:string_char_single* \"'\" { return nnode('string', [all.join('')], undefined, \"'\" + all.join('') + \"'\"); }",
- " / '\"' all:string_char_double* '\"' { return nnode('string', [all.join('')], undefined, '\"' + all.join('') + '\"'); }",
+ " = \"'\" all:string_char_single* \"'\" { return nnode('string', [all.join('')], column, undefined, \"'\" + all.join('') + \"'\"); }",
+ " / '\"' all:string_char_double* '\"' { return nnode('string', [all.join('')], column, undefined, '\"' + all.join('') + '\"'); }",
"nl",
- " = all:[\\n]+ { return nnode('nl', all); }",
+ " = all:[\\n]+ { return nnode('nl', all, column); }",
"keyword",
" = is !identifier_char",
View
1  src/interpreter.js
@@ -279,4 +279,5 @@
}
exports.Interpreter.resolve = resolve;
+ exports.Interpreter.evaluateValue = evaluateValue;
})(typeof exports === 'undefined' ? this.Isla : exports);
View
4 src/parser.js
@@ -18,7 +18,9 @@
Isla = window.Isla;
}
- var pegParser = pegjs.buildParser(Isla.Grammar.peg);
+ var pegParser = pegjs.buildParser(Isla.Grammar.peg, {
+ trackLineAndColumn: true
+ });
exports.Parser = {};

No commit comments for this range

Something went wrong with that request. Please try again.