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.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
View
1  Makefile
@@ -1,7 +1,6 @@
.PHONY: site
all:
- node src/typegrammar.js
node src/grammar.js
deps:
View
145 src/compile.js
@@ -412,6 +412,8 @@ var compile = function(source, env, aliases, opts) {
// Parse the file to an AST.
opts.log(1, 'Lex');
var tokens = lexer.tokenise(source, opts);
+ opts.log(2, 'Tokens');
+ opts.log(2, tokens);
opts.log(1, 'Parse');
var ast = parser.parse(tokens, opts);
opts.log(1, 'Macro expansion');
@@ -544,7 +546,7 @@ var nodeRepl = function(opts) {
// Include the standard library
var fs = require('fs');
var prelude = fs.readFileSync(path.dirname(__dirname) + '/lib/prelude.roy', 'utf8');
- vm.runInNewContext(compile(prelude, env).output, sandbox, 'eval');
+ vm.runInNewContext(compile(prelude, env, {}, {nodejs: true}).output, sandbox, 'eval');
repl.setPrompt('roy> ');
repl.on('close', function() {
stdin.destroy();
@@ -697,7 +699,8 @@ var main = function() {
// Meta-commands configuration
var opts = {
colorConsole: false,
- debug: 0
+ debug: 0,
+ log: function() {}
};
// Roy package information
@@ -718,72 +721,78 @@ var main = function() {
var browserModules = false;
var run = false;
var includePrelude = true;
- switch(argv[0]) {
- case "-v":
- case "--version":
- console.log("Roy: " + info.description);
- console.log(info.version);
- process.exit();
- break;
- case "--help":
- case "-h":
- console.log("Roy: " + info.description + "\n");
- console.log("-v : show current version");
- console.log("-r [file] : run Roy-code without JavaScript output");
- console.log("-p : run without prelude (standard library)");
- console.log("-c : colorful REPL mode");
- console.log("-d [level] : output debug information (optionally with a level e.g. 1,2,3 for more information)");
- console.log("-h : show this help");
- return;
- case "--stdio":
- source = '';
- process.stdin.resume();
- process.stdin.setEncoding('utf8');
- process.stdin.on('data', function(data) {
- source += data;
- });
- process.stdin.on('end', function() {
- console.log(compile(source).output);
- });
- return;
- case "-p":
- includePrelude = false;
- case "-r":
- vm = require('vm');
- run = true;
- argv.shift();
- break;
- case "-b":
- case "--browser":
- browserModules = true;
- argv.shift();
- break;
- case "-d":
- case "--debug":
- opts.debug = 1;
- argv.shift();
- if (Number(argv[0])) {
- opts.debug = Number(argv[0]);
+ while (argv[0][0]==='-'){
+ switch(argv[0]) {
+ case "-v":
+ case "--version":
+ console.log("Roy: " + info.description);
+ console.log(info.version);
+ process.exit();
+ break;
+ case "--help":
+ case "-h":
+ console.log("Roy: " + info.description + "\n");
+ console.log("-v : show current version");
+ console.log("-r [file] : run Roy-code without JavaScript output");
+ console.log("-p : run without prelude (standard library)");
+ console.log("-c : colorful REPL mode");
+ console.log("-d [level] : output debug information (optionally with a level e.g. 1,2,3 for more information)");
+ console.log("--stdout : output to stdout");
+ console.log("-h : show this help");
+ return;
+ case "--stdout":
+ opts.stdout = true;
argv.shift();
- }
- opts.log = function (level) {
- if (!Number(level)){
- throw new Error("Compiler error: Invalid level in opts.log call: " + level);
+ break;
+ case "--stdio":
+ source = '';
+ process.stdin.resume();
+ process.stdin.setEncoding('utf8');
+ process.stdin.on('data', function(data) {
+ source += data;
+ });
+ process.stdin.on('end', function() {
+ console.log(compile(source).output);
+ });
+ return;
+ case "-p":
+ includePrelude = false;
+ case "-r":
+ vm = require('vm');
+ run = true;
+ argv.shift();
+ break;
+ case "-b":
+ case "--browser":
+ browserModules = true;
+ argv.shift();
+ break;
+ case "-d":
+ case "--debug":
+ opts.debug = 1;
+ argv.shift();
+ if (Number(argv[0])) {
+ opts.debug = Number(argv[0]);
+ argv.shift();
}
- if (opts.debug >= level){
- spacing = '';
- for (var i=1;i<level;i++){spacing += ' '};
- console.log(spacing + _.values(arguments).slice(1,1000000).join(' ').toString());
+ opts.log = function (level) {
+ if (!Number(level)){
+ throw new Error("Compiler error: Invalid level in opts.log call: " + level);
+ }
+ if (opts.debug >= level){
+ spacing = '';
+ for (var i=1;i<level;i++){spacing += ' '};
+ console.log(spacing + _.values(arguments).slice(1,1000000).join(' ').toString());
+ }
}
+ break;
+ case "-c":
+ case "--color":
+ opts.colorConsole = true;
+ nodeRepl(opts);
+ return;
}
- break;
- case "-c":
- case "--color":
- opts.colorConsole = true;
- nodeRepl(opts);
- return;
}
-
var extensions = /\.l?roy$/;
var literateExtension = /\.lroy$/;
@@ -839,9 +848,13 @@ var main = function() {
output = vm.runInNewContext(compiled.output, sandbox, 'eval');
} else {
// Write the JavaScript output.
- fs.writeFile(outputPath, compiled.output + '//@ sourceMappingURL=' + path.basename(outputPath) + '.map\n', 'utf8');
- fs.writeFile(outputPath + '.map', sourceMap.toString(), 'utf8');
- writeModule(env, exported, filename.replace(extensions, '.roym'));
+ if (opts.stdout) {
+ console.log(compiled.output);
+ } else {
+ fs.writeFile(outputPath, compiled.output + '//@ sourceMappingURL=' + path.basename(outputPath) + '.map\n', 'utf8');
+ fs.writeFile(outputPath + '.map', sourceMap.toString(), 'utf8');
+ writeModule(env, exported, filename.replace(extensions, '.roym'));
+ }
}
});
};
View
312 src/grammar.js
@@ -1,6 +1,5 @@
var sys = require('sys'),
- Parser = require('jison').Parser,
- typegrammar = require('./typegrammar').bnf;
+ Parser = require('jison').Parser;
var n = function(s) {
return s + "$$.lineno = yylineno;"
@@ -21,6 +20,9 @@ var grammar = {
],
"bnf": {
+ ///////////////
+ // TOP LEVEL //
+ ///////////////
"program": [
["EOF", "return [];"],
["body EOF", "return $1;"]
@@ -38,6 +40,22 @@ var grammar = {
"block": [
["INDENT body outdentOrEof", "$$ = $2;"]
],
+ "macro": [
+ ["MACRO lowercase_identifier = expression", n("$$ = new yy.Macro($2, [$4]);")],
+ ["MACRO lowercase_identifier = block", n("$$ = new yy.Macro($2, $4);")]
+ ],
+ "function": [
+ ["lowercase_identifier paramList optType = block optWhere", n("$$ = new yy.Function($1, $2, $5, $3, $6);")],
+ ["lowercase_identifier paramList optType = expression", n("$$ = new yy.Function($1, $2, [$5], $3, []);")]
+ ],
+ "binding": [
+ ["lowercase_identifier optType = expression", n("$$ = new yy.Let($1, $4, $2);")],
+ ["lowercase_identifier optType = INDENT expression outdentOrEof", n("$$ = new yy.Let($1, $5, $2);")]
+ ],
+
+ /////////////////
+ // DO NOTATION //
+ // //////////////
"doBody": [
["doLine", "$$ = [$1];"],
["doBody TERMINATOR doLine", "$$ = $1; $1.push($3);"],
@@ -45,12 +63,16 @@ var grammar = {
],
"doLine": [
["line", "$$ = $1;"],
- ["IDENTIFIER LEFTARROW expression", n("$$ = new yy.Bind($1, $3);")],
+ ["lowercase_identifier LEFTARROW expression", n("$$ = new yy.Bind($1, $3);")],
["RETURN expression", n("$$ = new yy.Return($2);")]
],
"doBlock": [
["INDENT doBody outdentOrEof", "$$ = $2;"]
],
+
+ ////////////////
+ // STATEMENTS //
+ ////////////////
"statement": [
["LET function", "$$ = $2;"],
["LET binding", "$$ = $2;"],
@@ -60,6 +82,11 @@ var grammar = {
["instanceDecl", "$$ = $1;"],
["macro", "$$ = $1;"]
],
+
+ /////////////////
+ // EXPRESSIONS //
+ /////////////////
+
"expression": [
["innerExpression", "$$ = $1;"],
["LAMBDA paramList optType RIGHTARROW expression", n("$$ = new yy.Function(undefined, $2, [$5], $3);")],
@@ -68,6 +95,35 @@ var grammar = {
["DO innerExpression doBlock", n("$$ = new yy.Do($2, $3);")],
["ifThenElse", "$$ = $1;"]
],
+ "paramList": [
+ ["( )", "$$ = [];"],
+ ["param", "$$ = [$1];"],
+ ["paramList ( )", "$$ = $1;"],
+ ["paramList param", "$$ = $1; $1.push($2);"]
+ ],
+ "param": [
+ ["lowercase_identifier", n("$$ = new yy.Arg($1);")],
+ ["( lowercase_identifier : type )", n("$$ = new yy.Arg($2, $4);")]
+ ],
+"ifThenElse": [
+ ["IF innerExpression THEN block TERMINATOR ELSE block", n("$$ = new yy.IfThenElse($2, $4, $7);")],
+ ["IF innerExpression THEN innerExpression ELSE innerExpression", n("$$ = new yy.IfThenElse($2, [$4], [$6]);")]
+ ],
+ "innerExpression": [
+ ["call", "$$ = $1;"],
+ ["callArgument", "$$ = $1;"]
+ ],
+
+ "call": [
+ ["accessor argList", n("$$ = new yy.Call($1, $2);")],
+ ["( expression ) argList", n("$$ = new yy.Call($2, $4);")]
+ ],
+ "argList": [
+ ["( )", "$$ = [];"],
+ ["callArgument", "$$ = [$1];"],
+ ["argList ( )", "$$ = $1;"],
+ ["argList callArgument", "$$ = $1; $1.push($2);"]
+ ],
"callArgument": [
["( expression )", n("$$ = new yy.Expression($2);")],
["& ( expression )", n("$$ = new yy.Replacement($3);")],
@@ -83,96 +139,139 @@ var grammar = {
["callArgument WITH callArgument", n("$$ = new yy.With($1, $3);")],
["literal", "$$ = $1;"]
],
- "innerExpression": [
- ["call", "$$ = $1;"],
- ["callArgument", "$$ = $1;"]
+ "accessor": [
+ ["lowercase_identifier", n("$$ = new yy.Identifier($1);")],
+ ["accessor . keywordOrIdentifier", n("$$ = new yy.PropertyAccess($1, $3);")],
+ ["( expression ) . keywordOrIdentifier", n("$$ = new yy.PropertyAccess($2, $5);")]
],
+
+ ////////////////////
+ // CASE / PATTERN //
+ ////////////////////
"caseList": [
["CASE pattern = expression", "$$ = [new yy.Case($2, $4)];"],
["caseList TERMINATOR CASE pattern = expression", "$$ = $1; $1.push(new yy.Case($4, $6));"]
],
"pattern": [
- ["innerPattern", "$$ = $1;"],
- ["identifier", n("$$ = new yy.Pattern($1, []);")]
+ //["( pattern )", "$$ = $1;"],
+ ["literal", "$$ = new yy.Pattern($1, []);"],
+ ["dataConstructorInstance", n("$$ = new yy.Pattern($1, []);")],
+ ["lowercase_identifier", n("$$ = new yy.Pattern($1, []);")]
],
"innerPattern": [
["( identifier patternIdentifiers )", n("$$ = new yy.Pattern($2, $3);")]
],
"patternIdentifiers": [
- ["identifier", "$$ = [$1];"],
+ ["lowercase_identifier", "$$ = [$1];"],
["innerPattern", "$$ = [$1];"],
["patternIdentifiers innerPattern", "$$ = $1; $1.push($2);"],
["patternIdentifiers identifier", "$$ = $1; $1.push($2);"]
],
- "ifThenElse": [
- ["IF innerExpression THEN block TERMINATOR ELSE block", n("$$ = new yy.IfThenElse($2, $4, $7);")],
- ["IF innerExpression THEN innerExpression ELSE innerExpression", n("$$ = new yy.IfThenElse($2, [$4], [$6]);")]
- ],
+ ///////////////////////
+ // DATA DECLARATIONS //
+ ///////////////////////
+
// data Maybe a = Some a | None
"dataDecl": [
- ["DATA IDENTIFIER optDataParamList = dataList", n("$$ = new yy.Data($2, $3, $5);")],
- ["DATA IDENTIFIER optDataParamList = INDENT dataList outdentOrEof", n("$$ = new yy.Data($2, $3, $6);")]
+ ["DATA uppercase_identifier optDataParamList = dataList", n("$$ = new yy.Data($2, $3, $5);")],
+ ["DATA uppercase_identifier optDataParamList = INDENT dataList outdentOrEof", n("$$ = new yy.Data($2, $3, $6);")]
+ ],
+ "dataParamList": [
+ ["type", "$$ = [new yy.Arg($1)];"],
+ ["dataParamList type", "$$ = $1; $1.push(new yy.Arg($2));"],
+ ],
+ "optDataParamList": [
+ ["", "$$ = [];"],
+ ["dataParamList", "$$ = $1;"]
],
"dataList": [
- ["IDENTIFIER optTypeParamList", "$$ = [new yy.Tag($1, $2)];"],
- ["dataList | IDENTIFIER optTypeParamList", "$$ = $1; $1.push(new yy.Tag($3, $4));"]
+ ["dataConstructor", "$$ = $1;"],
+ ["dataList | dataConstructor", "$$ = $1; $1.push(new yy.Tag($3, $4));"]
],
+ // Some a
+ "dataConstructor": [
+ ["uppercase_identifier optTypeParamList", "$$ = [new yy.Tag($1, $2)];"]
+ ],
+ // Some expression
+ "dataConstructorInstance": [
+ ["uppercase_identifier optParamList", "$$ = [new yy.Tag($1, $2)];"],
+ ["dataConstructorInstance ( uppercase_identifier optParamList )", "$$ = $1; $1.push([new yy.Tag($1, $2)]);"]
+ ],
+ // expression
+ "dataConstructorOptParamList" : [
+ ["expression" , "$$ = [$1];"],
+ ["dataConstructorOptParamList expression" , "$$ = [$1];"]
+ ],
+
+ ///////////////////////
+ // TYPE DECLARATIONS //
+ ///////////////////////
+
// type Person = {firstName: String, lastName: String}
"typeDecl": [
- ["TYPE IDENTIFIER = type", n("$$ = new yy.Type($2, $4);")]
+ ["TYPE uppercase_identifier = type", n("$$ = new yy.Type($2, $4);")]
+ ],
+ "type": [
+ ["uppercase_identifier optTypeParamList", "$$ = new yy.TypeName($1, $2);"],
+ ["FUNCTION ( optTypeFunctionArgList )", "$$ = new yy.TypeFunction($3);"],
+ ["GENERIC", "$$ = new yy.Generic($1);"],
+ ["[ type ]", "$$ = new yy.TypeArray($2);"],
+ // type Person = (String, Number)
+ ["( typeTuple)", "$$ = new yy.TypeObject($2);"],
+ // type Person = {firstName: String, lastName: String}
+ ["{ optTypePairs }", "$$ = new yy.TypeObject($2);"]
+ ],
+ "typeTuple": [
+ ["type", "$$ = [$1];"],
+ ["typeTuple, type", "$$ = $1; $1.push($3);"]
+ ],
+ "optTypeParamList": [
+ ["", "$$ = [];"],
+ ["typeParamList", "$$ = $1;"]
+ ],
+ "typeParamList": [
+ //["uppercase_identifier", "$$ = [new yy.TypeName($1, [])];"],
+ //["GENERIC", "$$ = [new yy.Generic($1, [])];"],
+ //["( type )", "$$ = [$2];"],
+ ["typeParamList lowercase_identifier", "$$ = $1; $1.push(new yy.TypeName($2, []));"],
+ //["typeParamList GENERIC", "$$ = $1; $1.push(new yy.Generic($2, []));"],
+ //["typeParamList ( type )", "$$ = $1; $1.push($3);"]
+ ],
+ "optTypeFunctionArgList": [
+ ["", "$$ = [];"],
+ ["typeFunctionArgList", "$$ = $1;"]
+ ],
+ "typeFunctionArgList": [
+ ["type", "$$ = [$1];"],
+ ["typeFunctionArgList , type", "$$ = $1; $1.push($3);"]
+ ],
+ "optTypePairs": [
+ ["", "$$ = {};"],
+ ["keywordOrIdentifier : type", "$$ = {}; $$[$1] = $3;"],
+ ["optTypePairs , keywordOrIdentifier : type", "$$ = $1; $1[$3] = $5;"]
],
- // For type annotations (from the typegrammar module)
- "type": typegrammar.type,
- "typeList": typegrammar.typeList,
- "optTypeParamList": typegrammar.optTypeParamList,
- "typeParamList": typegrammar.typeParamList,
- "optTypeFunctionArgList": typegrammar.optTypeFunctionArgList,
- "typeFunctionArgList": typegrammar.typeFunctionArgList,
- "optTypePairs": typegrammar.optTypePairs,
- "dataParamList": typegrammar.dataParamList,
- "optDataParamList": typegrammar.optDataParamList,
+ /////////////////////////////
+ // TYPE CLASS DECLARATIONS //
+ /////////////////////////////
"typeClassDecl": [
- ["TYPECLASS IDENTIFIER GENERIC { INDENT typeClassLines outdentOrEof TERMINATOR }", "$$ = new yy.TypeClass($2, new yy.Generic($3), $6);"]
+ ["TYPECLASS uppercase_identifier GENERIC { INDENT typeClassLines outdentOrEof TERMINATOR }", "$$ = new yy.TypeClass($2, new yy.Generic($3), $6);"]
],
"typeClassLines": [
- ["IDENTIFIER : type", "$$ = {}; $$[$1] = $3;"],
- ["typeClassLines TERMINATOR IDENTIFIER : type", "$$ = $1; $1[$3] = $5;"]
+ ["lowercase_identifier : type", "$$ = {}; $$[$1] = $3;"],
+ ["typeClassLines TERMINATOR lowercase_identifier : type", "$$ = $1; $1[$3] = $5;"]
],
"instanceDecl": [
- ["INSTANCE IDENTIFIER = IDENTIFIER type object", "$$ = new yy.Instance($2, $4, $5, $6);"]
+ ["INSTANCE lowercase_identifier = uppercase_identifier type object", "$$ = new yy.Instance($2, $4, $5, $6);"]
],
- "macro": [
- ["MACRO IDENTIFIER = expression", n("$$ = new yy.Macro($2, [$4]);")],
- ["MACRO IDENTIFIER = block", n("$$ = new yy.Macro($2, $4);")]
- ],
- "function": [
- ["IDENTIFIER paramList optType = block optWhere", n("$$ = new yy.Function($1, $2, $5, $3, $6);")],
- ["IDENTIFIER paramList optType = expression", n("$$ = new yy.Function($1, $2, [$5], $3, []);")]
- ],
- "binding": [
- ["IDENTIFIER optType = expression", n("$$ = new yy.Let($1, $4, $2);")],
- ["IDENTIFIER optType = INDENT expression outdentOrEof", n("$$ = new yy.Let($1, $5, $2);")]
- ],
- "paramList": [
- ["( )", "$$ = [];"],
- ["param", "$$ = [$1];"],
- ["paramList ( )", "$$ = $1;"],
- ["paramList param", "$$ = $1; $1.push($2);"]
- ],
- "param": [
- ["IDENTIFIER", n("$$ = new yy.Arg($1);")],
- ["( IDENTIFIER : type )", n("$$ = new yy.Arg($2, $4);")]
- ],
- "optType": [
- ["", ""],
- [": type", "$$ = $2"]
- ],
+ ///////////
+ // WHERE //
+ ///////////
"optWhere": [
["", "$$ = [];"],
["WHERE INDENT whereDecls outdentOrEof", "$$ = $3;"]
@@ -183,47 +282,28 @@ var grammar = {
],
"whereDecl": [
["dataDecl", "$$ = $1;"],
- ["IDENTIFIER paramList optType = block optWhere", n("$$ = new yy.Function($1, $2, $5, $3, $6);")],
- ["IDENTIFIER paramList optType = expression", n("$$ = new yy.Function($1, $2, [$5], $3, []);")]
+ ["lowercase_identifier paramList optType = block optWhere", n("$$ = new yy.Function($1, $2, $5, $3, $6);")],
+ ["lowercase_identifier paramList optType = expression", n("$$ = new yy.Function($1, $2, [$5], $3, []);")]
],
- "call": [
- ["accessor argList", n("$$ = new yy.Call($1, $2);")],
- ["( expression ) argList", n("$$ = new yy.Call($2, $4);")]
- ],
- "argList": [
- ["( )", "$$ = [];"],
- ["callArgument", "$$ = [$1];"],
- ["argList ( )", "$$ = $1;"],
- ["argList callArgument", "$$ = $1; $1.push($2);"]
- ],
- "tuple": [
- ["( innerExpression , tupleList )", n("$4.unshift($2); $$ = new yy.Tuple($4);")]
- ],
- "tupleList": [
- ["innerExpression", "$$ = [$1];"],
- ["tupleList , innerExpression", "$$ = $1; $1.push($3);"]
- ],
+
+
+ //////////////
+ // LITERALS //
+ //////////////
+
"literal": [
["NUMBER", n("$$ = new yy.Number($1);")],
["STRING", n("$$ = new yy.String($1);")],
["BOOLEAN", n("$$ = new yy.Boolean($1);")],
["tuple", "$$ = $1;"],
- ["[ optValues ]", n("$$ = new yy.Array($2);")],
+ ["array", n("$$ = new yy.Array($2);")],
["object", "$$ = $1;"]
],
"object": [
- ["{ optPairs }", n("$$ = new yy.Object($2);")]
+ ["{ optKeyPairs }", n("$$ = new yy.Object($2);")]
],
- "optValues": [
- ["", "$$ = [];"],
- ["arrayValues", "$$ = $1;"]
- ],
- "arrayValues": [
- ["expression", "$$ = [$1];"],
- ["arrayValues , expression", "$$ = $1; $1.push($3);"]
- ],
- "optPairs": [
+ "optKeyPairs": [
["", "$$ = {};"],
["INDENT keyPairs OUTDENT TERMINATOR", "$$ = $2;"],
["keyPairs", "$$ = $1;"]
@@ -233,24 +313,60 @@ var grammar = {
["keyPairs , keywordOrIdentifier : expression", "$$ = $1; $1[$3] = $5;"],
["keyPairs TERMINATOR optTerm keywordOrIdentifier : expression", "$$ = $1; $1[$4] = $6;"]
],
+ "tuple": [
+ ["( innerExpression , tupleList )", n("$4.unshift($2); $$ = new yy.Tuple($4);")]
+ ],
+ "tupleList": [
+ ["innerExpression", "$$ = [$1];"],
+ ["tupleList , innerExpression", "$$ = $1; $1.push($3);"]
+ ],
+ "array": [
+ ["[ optArrayValues ]", n("$$ = new yy.Array($2);")],
+ ],
+ "optArrayValues": [
+ ["", "$$ = [];"],
+ ["arrayValues", "$$ = $1;"]
+ ],
+ "arrayValues": [
+ ["expression", "$$ = [$1];"],
+ ["arrayValues , expression", "$$ = $1; $1.push($3);"]
+ ],
+
+ ////////////////////
+ // SHARED GRAMMAR //
+ ////////////////////
+ "optType": [
+ ["", ""],
+ [": type", "$$ = $2"]
+ ],
"optTerm": [
["", ""],
["TERMINATOR", ""]
],
- "accessor": [
- ["IDENTIFIER", n("$$ = new yy.Identifier($1);")],
- ["accessor . keywordOrIdentifier", n("$$ = new yy.PropertyAccess($1, $3);")],
- ["( expression ) . keywordOrIdentifier", n("$$ = new yy.PropertyAccess($2, $5);")]
- ],
"outdentOrEof": [
["OUTDENT", ""],
["EOF", ""]
],
- "keywordOrIdentifier": typegrammar.keywordOrIdentifier,
- "identifier": [
- ["IDENTIFIER", n("$$ = new yy.Identifier($1);")]
- ]
- }
+ "lowercase_identifier": [
+ ["LOWERCASE_IDENTIFIER", n("$$ = new yy.Identifier($1);")]
+ ],
+ "uppercase_identifier": [
+ ["UPPERCASE_IDENTIFIER", n("$$ = new yy.Identifier($1);")]
+ ],
+ "keywordOrIdentifier": [
+ ["THEN", "$$ = $1;"],
+ ["ELSE", "$$ = $1;"],
+ ["DATA", "$$ = $1;"],
+ ["TYPE", "$$ = $1;"],
+ ["MATCH", "$$ = $1;"],
+ ["CASE", "$$ = $1;"],
+ ["DO", "$$ = $1;"],
+ ["RETURN", "$$ = $1;"],
+ ["MACRO", "$$ = $1;"],
+ ["WITH", "$$ = $1;"],
+ ["WHERE", "$$ = $1;"],
+ ["lowercase_identifier", "$$ = $1;"]
+ ] }
};
var parser = new Parser(grammar, {debug: true});
View
18 src/lexer.js
@@ -4,10 +4,17 @@ var unicode = require('unicode-categories');
// ECMAscript identifier starts with `$`, `_`,
// or letter from (Lu Ll Lt Lm Lo Nl) unicode groups.
// Then identifier can also be from groups (Nd, Mn, Mc, or Pc).
+
// Roy identifier cannot have letter u03BB (greek lowercase lambda)
// because it's used in anonymous functions.
+var FORBIDDEN_CHARACTERS = /\u03BB/g;
+
var IDENTIFIER = new RegExp(
- unicode.ECMA.identifier.source.replace('\\u03BB', '')
+ unicode.ECMA.identifier.source.replace(FORBIDDEN_CHARACTERS, '')
+);
+
+var UPPERCASE = new RegExp(
+ ('^' + unicode.Lu.source).replace(FORBIDDEN_CHARACTERS, '')
);
var NUMBER = /^-?[0-9]+(\.[0-9]+)?/;
@@ -52,7 +59,10 @@ var identifierToken = function() {
name = value.toUpperCase();
break;
default:
- name = 'IDENTIFIER';
+ name = 'LOWERCASE_IDENTIFIER';
+ if (value.match(UPPERCASE)) {
+ name = 'UPPERCASE_IDENTIFIER';
+ }
break;
}
tokens.push([name, value, lineno]);
@@ -277,7 +287,9 @@ exports.tokenise = function(source) {
lineno = 0;
var i = 0;
while(chunk = source.slice(i)) {
- var diff = identifierToken() || numberToken() || stringToken() || genericToken() || commentToken() || whitespaceToken() || lineToken() || literalToken();
+ var diff = commentToken() || literalToken() || identifierToken() || numberToken() || stringToken() ||
+ genericToken() || whitespaceToken() ||
+ lineToken();
if(!diff) {
throw "Couldn't tokenise: " + chunk.substring(0, chunk.indexOf("\n") > -1 ? chunk.indexOf("\n") : chunk.length);
}
View
4 src/macroexpand.js
@@ -17,8 +17,8 @@ var macroexpand = function(ast, env, opts) {
visitCall: function() {
if(!macros[n.func.value]) return;
- var f = new Function(macros[n.func.value]);
- var tree = f.apply({}, n.args);
+ var f = new Function('var nodes = this.nodes; ' + macros[n.func.value]);
+ var tree = f.apply({nodes: nodes}, n.args);
return tree;
}
View
105 src/typegrammar.js
@@ -1,105 +0,0 @@
-var Parser = require('jison').Parser;
-
-var bnf = {
- // For type annotations
- "type": [
- ["IDENTIFIER optTypeParamList", "$$ = new yy.TypeName($1, $2);"],
- ["FUNCTION ( optTypeFunctionArgList )", "$$ = new yy.TypeFunction($3);"],
- ["GENERIC", "$$ = new yy.Generic($1);"],
- ["[ type ]", "$$ = new yy.TypeArray($2);"],
- ["( typeList )", "$$ = new yy.TypeObject($2);"],
- ["{ optTypePairs }", "$$ = new yy.TypeObject($2);"]
- ],
- "typeList": [
- ["type", "$$ = [$1];"],
- ["typeList , type", "$$ = $1; $1.push($3);"]
- ],
- "optTypeParamList": [
- ["", "$$ = [];"],
- ["typeParamList", "$$ = $1;"]
- ],
- "typeParamList": [
- ["IDENTIFIER", "$$ = [new yy.TypeName($1, [])];"],
- ["GENERIC", "$$ = [new yy.Generic($1, [])];"],
- ["( type )", "$$ = [$2];"],
- ["typeParamList IDENTIFIER", "$$ = $1; $1.push(new yy.TypeName($2, []));"],
- ["typeParamList GENERIC", "$$ = $1; $1.push(new yy.Generic($2, []));"],
- ["typeParamList ( type )", "$$ = $1; $1.push($3);"]
- ],
- "optTypeFunctionArgList": [
- ["", "$$ = [];"],
- ["typeFunctionArgList", "$$ = $1;"]
- ],
- "typeFunctionArgList": [
- ["type", "$$ = [$1];"],
- ["typeFunctionArgList , type", "$$ = $1; $1.push($3);"]
- ],
- "optTypePairs": [
- ["", "$$ = {};"],
- ["keywordOrIdentifier : type", "$$ = {}; $$[$1] = $3;"],
- ["optTypePairs , keywordOrIdentifier : type", "$$ = $1; $1[$3] = $5;"]
- ],
- "dataParamList": [
- ["IDENTIFIER", "$$ = [new yy.Arg($1)];"],
- ["dataParamList IDENTIFIER", "$$ = $1; $1.push(new yy.Arg($2));"],
- ],
- "optDataParamList": [
- ["", "$$ = [];"],
- ["dataParamList", "$$ = $1;"]
- ],
- "keywordOrIdentifier": [
- ["THEN", "$$ = $1;"],
- ["ELSE", "$$ = $1;"],
- ["DATA", "$$ = $1;"],
- ["TYPE", "$$ = $1;"],
- ["MATCH", "$$ = $1;"],
- ["CASE", "$$ = $1;"],
- ["DO", "$$ = $1;"],
- ["RETURN", "$$ = $1;"],
- ["MACRO", "$$ = $1;"],
- ["WITH", "$$ = $1;"],
- ["WHERE", "$$ = $1;"],
- ["IDENTIFIER", "$$ = $1;"]
- ]
-};
-
-exports.bnf = bnf;
-
-var grammar = {
- "startSymbol": "typefile",
-
- "bnf": {
- "typefile": [
- ["EOF", "return {};"],
- ["body EOF", "return $1;"]
- ],
- "body": [
- ["pair", "$$ = {types: {}, env: {}}; if($1.data) { $$.types[$1.name] = $1.params; } else { $$.env[$1.name] = $1.type; }"],
- ["body TERMINATOR pair", "$$ = $1; if($3.data) { $$.types[$3.name] = $3.params; } else { $$.env[$3.name] = $3.type; }"],
- ["body TERMINATOR", "$$ = $1;"]
- ],
-
- "pair": [
- ["IDENTIFIER : type", "$$ = {name: $1, type: $3, data: false};"],
- ["TYPE IDENTIFIER optDataParamList", "$$ = {name: $2, params: $3, data: true};"]
- ],
-
- "type": bnf.type,
- "typeList": bnf.typeList,
- "optTypeParamList": bnf.optTypeParamList,
- "typeParamList": bnf.typeParamList,
- "optTypeFunctionArgList": bnf.optTypeFunctionArgList,
- "typeFunctionArgList": bnf.typeFunctionArgList,
- "optTypePairs": bnf.optTypePairs,
- "dataParamList": bnf.dataParamList,
- "optDataParamList": bnf.optDataParamList,
- "keywordOrIdentifier": bnf.keywordOrIdentifier
- }
-};
-
-if(exports && !module.parent) {
- var parser = new Parser(grammar, {debug: true});
-
- var fs = require('fs');
- fs.writeFile('src/typeparser.js', parser.generate());
-}

No commit comments for this range

Something went wrong with that request. Please try again.