Add initial support for packages and imports

This commit is contained in:
Sam Vervaeck 2020-05-25 11:29:19 +02:00
parent 53b253b9e9
commit 17d91ef36c
17 changed files with 645 additions and 412 deletions

View file

@ -1,14 +1,14 @@
TREEGEN_FILES = spec/ast.txt lib/bin/bolt-treegen.js lib/treegen/parser.js lib/treegen/index.js lib/treegen/util.js src/treegen/ast-template.js TREEGEN_FILES = src/ast-spec.txt lib/bin/bolt-treegen.js lib/treegen/parser.js lib/treegen/index.js lib/treegen/util.js src/treegen/ast-template.js
all: lib/ast.js all: lib/ast.js
bolt bundle test.bolt bolt bundle stdlib
lib/ast.js: $(TREEGEN_FILES) lib/ast.js: $(TREEGEN_FILES)
@echo "Generating AST definitions ..." @echo "Generating AST definitions ..."
@mkdir -p lib/ @mkdir -p lib/
@chmod +x lib/bin/*.js @chmod +x lib/bin/*.js
@bolt-treegen --js-file=lib/ast.js --dts-file src/ast.d.ts spec/ast.txt @bolt-treegen --js-file=lib/ast.js --dts-file src/ast.d.ts src/ast-spec.txt
lib/treegen/parser.js: src/treegen/parser.pegjs lib/treegen/parser.js: src/treegen/parser.pegjs
@echo "Generating parser ..." @echo "Generating parser ..."

52
package-lock.json generated
View file

@ -38,6 +38,11 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/js-yaml": {
"version": "3.12.4",
"resolved": "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-3.12.4.tgz",
"integrity": "sha512-fYMgzN+9e28R81weVN49inn/u798ruU91En1ZnGvSZzCRc5jXx9B2EDhlRaWmcO1RIxFHL8AajRXzxDuJu93+A=="
},
"@types/microtime": { "@types/microtime": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/@types/microtime/-/microtime-2.1.0.tgz", "resolved": "https://registry.npmjs.org/@types/microtime/-/microtime-2.1.0.tgz",
@ -64,6 +69,14 @@
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.5.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.5.tgz",
"integrity": "sha512-90hiq6/VqtQgX8Sp0EzeIsv3r+ellbGj4URKj5j30tLlZvRUpnAe9YbYnjl3pJM93GyXU0tghHhvXHq+5rnCKA==" "integrity": "sha512-90hiq6/VqtQgX8Sp0EzeIsv3r+ellbGj4URKj5j30tLlZvRUpnAe9YbYnjl3pJM93GyXU0tghHhvXHq+5rnCKA=="
}, },
"@types/semver": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.2.0.tgz",
"integrity": "sha512-TbB0A8ACUWZt3Y6bQPstW9QNbhNeebdgLX4T/ZfkrswAfUzRiXrgd9seol+X379Wa589Pu4UEx9Uok0D4RjRCQ==",
"requires": {
"@types/node": "*"
}
},
"@types/yargs": { "@types/yargs": {
"version": "15.0.5", "version": "15.0.5",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.5.tgz", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.5.tgz",
@ -111,7 +124,6 @@
"version": "1.0.10", "version": "1.0.10",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
"integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
"dev": true,
"requires": { "requires": {
"sprintf-js": "~1.0.2" "sprintf-js": "~1.0.2"
} }
@ -353,8 +365,7 @@
"esprima": { "esprima": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A=="
"dev": true
}, },
"fill-range": { "fill-range": {
"version": "7.0.1", "version": "7.0.1",
@ -576,10 +587,9 @@
"dev": true "dev": true
}, },
"js-yaml": { "js-yaml": {
"version": "3.13.1", "version": "3.14.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz",
"integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", "integrity": "sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==",
"dev": true,
"requires": { "requires": {
"argparse": "^1.0.7", "argparse": "^1.0.7",
"esprima": "^4.0.0" "esprima": "^4.0.0"
@ -741,6 +751,16 @@
"path-is-absolute": "^1.0.0" "path-is-absolute": "^1.0.0"
} }
}, },
"js-yaml": {
"version": "3.13.1",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz",
"integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==",
"dev": true,
"requires": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
}
},
"string-width": { "string-width": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
@ -825,6 +845,14 @@
"requires": { "requires": {
"object.getownpropertydescriptors": "^2.0.3", "object.getownpropertydescriptors": "^2.0.3",
"semver": "^5.7.0" "semver": "^5.7.0"
},
"dependencies": {
"semver": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
"dev": true
}
} }
}, },
"node-gyp-build": { "node-gyp-build": {
@ -955,10 +983,9 @@
"integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==" "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg=="
}, },
"semver": { "semver": {
"version": "5.7.1", "version": "7.3.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.2.tgz",
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "integrity": "sha512-OrOb32TeeambH6UrhtShmF7CRDqhL6/5XpPNp2DuRH6+9QLw/orhp72j87v8Qa1ScDkvrrBNpZcDejAirJmfXQ=="
"dev": true
}, },
"set-blocking": { "set-blocking": {
"version": "2.0.0", "version": "2.0.0",
@ -982,8 +1009,7 @@
"sprintf-js": { "sprintf-js": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
"dev": true
}, },
"string-width": { "string-width": {
"version": "4.2.0", "version": "4.2.0",

View file

@ -16,18 +16,22 @@
"dependencies": { "dependencies": {
"@types/fs-extra": "^9.0.1", "@types/fs-extra": "^9.0.1",
"@types/glob": "^7.1.1", "@types/glob": "^7.1.1",
"@types/js-yaml": "^3.12.4",
"@types/microtime": "^2.1.0", "@types/microtime": "^2.1.0",
"@types/minimist": "^1.2.0", "@types/minimist": "^1.2.0",
"@types/node": "^14.0.5", "@types/node": "^14.0.5",
"@types/semver": "^7.2.0",
"@types/yargs": "^15.0.5", "@types/yargs": "^15.0.5",
"chalk": "^4.0.0", "chalk": "^4.0.0",
"fs-extra": "^9.0.0", "fs-extra": "^9.0.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"js-yaml": "^3.14.0",
"microtime": "^3.0.0", "microtime": "^3.0.0",
"minimist": "^1.2.5", "minimist": "^1.2.5",
"moment": "^2.26.0", "moment": "^2.26.0",
"pegjs": "^0.11.0-master.b7b87ea", "pegjs": "^0.11.0-master.b7b87ea",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"semver": "^7.3.2",
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"yargs": "^15.3.1" "yargs": "^15.3.1"
}, },

View file

@ -67,6 +67,7 @@ node BoltLoopKeyword > BoltToken, BoltKeyword;
node BoltYieldKeyword > BoltToken, BoltKeyword; node BoltYieldKeyword > BoltToken, BoltKeyword;
node BoltMatchKeyword > BoltToken, BoltKeyword; node BoltMatchKeyword > BoltToken, BoltKeyword;
node BoltImportKeyword > BoltToken, BoltKeyword; node BoltImportKeyword > BoltToken, BoltKeyword;
node BoltExportKeyword > BoltToken, BoltKeyword;
node BoltPubKeyword > BoltToken, BoltKeyword; node BoltPubKeyword > BoltToken, BoltKeyword;
node BoltModKeyword > BoltToken, BoltKeyword; node BoltModKeyword > BoltToken, BoltKeyword;
node BoltMutKeyword > BoltToken, BoltKeyword; node BoltMutKeyword > BoltToken, BoltKeyword;
@ -86,6 +87,7 @@ node BoltBracketed > BoltPunctuated;
node BoltSourceFile > SourceFile { node BoltSourceFile > SourceFile {
elements: Vec<BoltSourceElement>, elements: Vec<BoltSourceElement>,
package: Package,
} }
node BoltQualName { node BoltQualName {
@ -237,15 +239,13 @@ node BoltDeclaration > BoltSourceElement;
node BoltTypeDeclaration > BoltSourceElement; node BoltTypeDeclaration > BoltSourceElement;
enum BoltDeclarationModifiers { enum BoltModifiers {
Mutable = 0x1, IsMutable = 0x1,
Public = 0x2, IsPublic = 0x2,
IsType = 0x4,
IsForeign = 0x8,
} }
node BoltModule > BoltDeclaration { node BoltModule > BoltSourceElement {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
name: BoltQualName, name: BoltQualName,
elements: Vec<BoltSourceElement>, elements: Vec<BoltSourceElement>,
} }
@ -253,7 +253,7 @@ node BoltModule > BoltDeclaration {
node BoltFunctionBodyElement; node BoltFunctionBodyElement;
node BoltFunctionDeclaration > BoltFunctionBodyElement, BoltDeclaration { node BoltFunctionDeclaration > BoltFunctionBodyElement, BoltDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
target: String, target: String,
name: BoltSymbol, name: BoltSymbol,
params: Vec<BoltParameter>, params: Vec<BoltParameter>,
@ -263,7 +263,7 @@ node BoltFunctionDeclaration > BoltFunctionBodyElement, BoltDeclaration {
} }
node BoltVariableDeclaration > BoltFunctionBodyElement, BoltDeclaration { node BoltVariableDeclaration > BoltFunctionBodyElement, BoltDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
bindings: BoltPattern, bindings: BoltPattern,
type: Option<BoltTypeExpression>, type: Option<BoltTypeExpression>,
value: Option<BoltExpression>, value: Option<BoltExpression>,
@ -275,20 +275,32 @@ node BoltPlainImportSymbol > BoltImportSymbol {
name: BoltQualName, name: BoltQualName,
} }
node BoltImportDeclaration > BoltDeclaration { node BoltImportDirective > BoltSourceElement {
modifiers: BoltModifiers,
file: String, file: String,
symbols: Vec<BoltImportSymbol>, symbols: Vec<BoltImportSymbol>,
} }
node BoltExportSymbol;
node BoltPlainExportSymbol {
name: BoltQualName,
}
node BoltExportDirective > BoltSourceElement {
file: String,
symbols: Option<Vec<BoltExportSymbol>>,
}
node BoltTraitDeclaration > BoltDeclaration, BoltTypeDeclaration { node BoltTraitDeclaration > BoltDeclaration, BoltTypeDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
name: BoltIdentifier, name: BoltIdentifier,
typeParams: Option<Vec<BoltTypeParameter>>, typeParams: Option<Vec<BoltTypeParameter>>,
elements: Vec<BoltDeclaration>, elements: Vec<BoltDeclaration>,
} }
node BoltImplDeclaration > BoltDeclaration { node BoltImplDeclaration > BoltDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
name: BoltIdentifier, name: BoltIdentifier,
trait: BoltTypeExpression, trait: BoltTypeExpression,
typeParams: Option<Vec<BoltTypeParameter>>, typeParams: Option<Vec<BoltTypeParameter>>,
@ -296,7 +308,7 @@ node BoltImplDeclaration > BoltDeclaration {
} }
node BoltTypeAliasDeclaration > BoltDeclaration, BoltTypeDeclaration { node BoltTypeAliasDeclaration > BoltDeclaration, BoltTypeDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
name: BoltIdentifier, name: BoltIdentifier,
typeParams: Option<Vec<BoltTypeParameter>>, typeParams: Option<Vec<BoltTypeParameter>>,
typeExpr: BoltTypeExpression, typeExpr: BoltTypeExpression,
@ -310,7 +322,7 @@ node BoltRecordField > BoltRecordMember {
} }
node BoltRecordDeclaration > BoltDeclaration, BoltTypeDeclaration { node BoltRecordDeclaration > BoltDeclaration, BoltTypeDeclaration {
modifiers: BoltDeclarationModifiers, modifiers: BoltModifiers,
name: BoltIdentifier, name: BoltIdentifier,
typeParms: Option<Vec<BoltTypeParameter>>, typeParms: Option<Vec<BoltTypeParameter>>,
members: Option<Vec<BoltRecordMember>>, members: Option<Vec<BoltRecordMember>>,

335
src/ast.d.ts vendored
View file

@ -1,4 +1,9 @@
import { Package } from "./common"
export function isSyntax(value: any): value is Syntax;
export const enum SyntaxKind { export const enum SyntaxKind {
EndOfFile = 2, EndOfFile = 2,
FunctionBody = 6, FunctionBody = 6,
@ -32,121 +37,125 @@ export const enum SyntaxKind {
BoltYieldKeyword = 38, BoltYieldKeyword = 38,
BoltMatchKeyword = 39, BoltMatchKeyword = 39,
BoltImportKeyword = 40, BoltImportKeyword = 40,
BoltPubKeyword = 41, BoltExportKeyword = 41,
BoltModKeyword = 42, BoltPubKeyword = 42,
BoltMutKeyword = 43, BoltModKeyword = 43,
BoltEnumKeyword = 44, BoltMutKeyword = 44,
BoltStructKeyword = 45, BoltEnumKeyword = 45,
BoltTypeKeyword = 46, BoltStructKeyword = 46,
BoltTraitKeyword = 47, BoltTypeKeyword = 47,
BoltImplKeyword = 48, BoltTraitKeyword = 48,
BoltParenthesized = 50, BoltImplKeyword = 49,
BoltBraced = 51, BoltParenthesized = 51,
BoltBracketed = 52, BoltBraced = 52,
BoltSourceFile = 53, BoltBracketed = 53,
BoltQualName = 54, BoltSourceFile = 54,
BoltReferenceTypeExpression = 56, BoltQualName = 55,
BoltFunctionTypeExpression = 57, BoltReferenceTypeExpression = 57,
BoltTypeParameter = 58, BoltFunctionTypeExpression = 58,
BoltBindPattern = 60, BoltTypeParameter = 59,
BoltTypePattern = 61, BoltBindPattern = 61,
BoltExpressionPattern = 62, BoltTypePattern = 62,
BoltTuplePatternElement = 63, BoltExpressionPattern = 63,
BoltTuplePattern = 64, BoltTuplePatternElement = 64,
BoltRecordFieldPattern = 65, BoltTuplePattern = 65,
BoltRecordPattern = 66, BoltRecordFieldPattern = 66,
BoltQuoteExpression = 68, BoltRecordPattern = 67,
BoltReferenceExpression = 69, BoltQuoteExpression = 69,
BoltMemberExpression = 70, BoltReferenceExpression = 70,
BoltFunctionExpression = 71, BoltMemberExpression = 71,
BoltCallExpression = 72, BoltFunctionExpression = 72,
BoltYieldExpression = 73, BoltCallExpression = 73,
BoltMatchArm = 74, BoltYieldExpression = 74,
BoltMatchExpression = 75, BoltMatchArm = 75,
BoltCase = 76, BoltMatchExpression = 76,
BoltCaseExpression = 77, BoltCase = 77,
BoltBlockExpression = 78, BoltCaseExpression = 78,
BoltConstantExpression = 79, BoltBlockExpression = 79,
BoltReturnStatement = 81, BoltConstantExpression = 80,
BoltConditionalCase = 82, BoltReturnStatement = 82,
BoltConditionalStatement = 83, BoltConditionalCase = 83,
BoltResumeStatement = 84, BoltConditionalStatement = 84,
BoltExpressionStatement = 85, BoltResumeStatement = 85,
BoltParameter = 86, BoltExpressionStatement = 86,
BoltModule = 90, BoltParameter = 87,
BoltFunctionDeclaration = 92, BoltModule = 91,
BoltVariableDeclaration = 93, BoltFunctionDeclaration = 93,
BoltPlainImportSymbol = 95, BoltVariableDeclaration = 94,
BoltImportDeclaration = 96, BoltPlainImportSymbol = 96,
BoltTraitDeclaration = 97, BoltImportDirective = 97,
BoltImplDeclaration = 98, BoltExportSymbol = 98,
BoltTypeAliasDeclaration = 99, BoltPlainExportSymbol = 99,
BoltRecordField = 101, BoltExportDirective = 100,
BoltRecordDeclaration = 102, BoltTraitDeclaration = 101,
BoltMacroCall = 104, BoltImplDeclaration = 102,
JSOperator = 107, BoltTypeAliasDeclaration = 103,
JSIdentifier = 108, BoltRecordField = 105,
JSString = 109, BoltRecordDeclaration = 106,
JSInteger = 110, BoltMacroCall = 108,
JSFromKeyword = 111, JSOperator = 111,
JSReturnKeyword = 112, JSIdentifier = 112,
JSTryKeyword = 113, JSString = 113,
JSFinallyKeyword = 114, JSInteger = 114,
JSCatchKeyword = 115, JSFromKeyword = 115,
JSImportKeyword = 116, JSReturnKeyword = 116,
JSAsKeyword = 117, JSTryKeyword = 117,
JSConstKeyword = 118, JSFinallyKeyword = 118,
JSLetKeyword = 119, JSCatchKeyword = 119,
JSExportKeyword = 120, JSImportKeyword = 120,
JSFunctionKeyword = 121, JSAsKeyword = 121,
JSWhileKeyword = 122, JSConstKeyword = 122,
JSForKeyword = 123, JSLetKeyword = 123,
JSCloseBrace = 124, JSExportKeyword = 124,
JSCloseBracket = 125, JSFunctionKeyword = 125,
JSCloseParen = 126, JSWhileKeyword = 126,
JSOpenBrace = 127, JSForKeyword = 127,
JSOpenBracket = 128, JSCloseBrace = 128,
JSOpenParen = 129, JSCloseBracket = 129,
JSSemi = 130, JSCloseParen = 130,
JSComma = 131, JSOpenBrace = 131,
JSDot = 132, JSOpenBracket = 132,
JSDotDotDot = 133, JSOpenParen = 133,
JSMulOp = 134, JSSemi = 134,
JSAddOp = 135, JSComma = 135,
JSDivOp = 136, JSDot = 136,
JSSubOp = 137, JSDotDotDot = 137,
JSLtOp = 138, JSMulOp = 138,
JSGtOp = 139, JSAddOp = 139,
JSBOrOp = 140, JSDivOp = 140,
JSBXorOp = 141, JSSubOp = 141,
JSBAndOp = 142, JSLtOp = 142,
JSBNotOp = 143, JSGtOp = 143,
JSNotOp = 144, JSBOrOp = 144,
JSBindPattern = 146, JSBXorOp = 145,
JSConstantExpression = 148, JSBAndOp = 146,
JSMemberExpression = 149, JSBNotOp = 147,
JSCallExpression = 150, JSNotOp = 148,
JSBinaryExpression = 151, JSBindPattern = 150,
JSUnaryExpression = 152, JSConstantExpression = 152,
JSNewExpression = 153, JSMemberExpression = 153,
JSSequenceExpression = 154, JSCallExpression = 154,
JSConditionalExpression = 155, JSBinaryExpression = 155,
JSLiteralExpression = 157, JSUnaryExpression = 156,
JSReferenceExpression = 158, JSNewExpression = 157,
JSCatchBlock = 162, JSSequenceExpression = 158,
JSTryCatchStatement = 163, JSConditionalExpression = 159,
JSExpressionStatement = 164, JSLiteralExpression = 161,
JSConditionalCase = 165, JSReferenceExpression = 162,
JSConditionalStatement = 166, JSCatchBlock = 166,
JSReturnStatement = 167, JSTryCatchStatement = 167,
JSParameter = 168, JSExpressionStatement = 168,
JSImportStarBinding = 172, JSConditionalCase = 169,
JSImportAsBinding = 173, JSConditionalStatement = 170,
JSImportDeclaration = 174, JSReturnStatement = 171,
JSFunctionDeclaration = 175, JSParameter = 172,
JSArrowFunctionDeclaration = 176, JSImportStarBinding = 176,
JSLetDeclaration = 177, JSImportAsBinding = 177,
JSSourceFile = 178, JSImportDeclaration = 178,
JSFunctionDeclaration = 179,
JSArrowFunctionDeclaration = 180,
JSLetDeclaration = 181,
JSSourceFile = 182,
} }
@ -158,6 +167,7 @@ export function setParents(node: Syntax): void;
export type SyntaxRange = [Syntax, Syntax]; export type SyntaxRange = [Syntax, Syntax];
interface SyntaxBase<K extends SyntaxKind> { interface SyntaxBase<K extends SyntaxKind> {
id: number;
kind: K; kind: K;
parentNode: ParentTypesOf<K> | null; parentNode: ParentTypesOf<K> | null;
span: TextSpan | null; span: TextSpan | null;
@ -207,6 +217,7 @@ export type Token
| BoltYieldKeyword | BoltYieldKeyword
| BoltMatchKeyword | BoltMatchKeyword
| BoltImportKeyword | BoltImportKeyword
| BoltExportKeyword
| BoltPubKeyword | BoltPubKeyword
| BoltModKeyword | BoltModKeyword
| BoltMutKeyword | BoltMutKeyword
@ -304,6 +315,7 @@ export type BoltToken
| BoltYieldKeyword | BoltYieldKeyword
| BoltMatchKeyword | BoltMatchKeyword
| BoltImportKeyword | BoltImportKeyword
| BoltExportKeyword
| BoltPubKeyword | BoltPubKeyword
| BoltModKeyword | BoltModKeyword
| BoltMutKeyword | BoltMutKeyword
@ -426,6 +438,7 @@ export type BoltKeyword
| BoltYieldKeyword | BoltYieldKeyword
| BoltMatchKeyword | BoltMatchKeyword
| BoltImportKeyword | BoltImportKeyword
| BoltExportKeyword
| BoltPubKeyword | BoltPubKeyword
| BoltModKeyword | BoltModKeyword
| BoltMutKeyword | BoltMutKeyword
@ -480,6 +493,10 @@ export interface BoltImportKeyword extends SyntaxBase<SyntaxKind.BoltImportKeywo
kind: SyntaxKind.BoltImportKeyword; kind: SyntaxKind.BoltImportKeyword;
} }
export interface BoltExportKeyword extends SyntaxBase<SyntaxKind.BoltExportKeyword> {
kind: SyntaxKind.BoltExportKeyword;
}
export interface BoltPubKeyword extends SyntaxBase<SyntaxKind.BoltPubKeyword> { export interface BoltPubKeyword extends SyntaxBase<SyntaxKind.BoltPubKeyword> {
kind: SyntaxKind.BoltPubKeyword; kind: SyntaxKind.BoltPubKeyword;
} }
@ -536,6 +553,7 @@ export interface BoltBracketed extends SyntaxBase<SyntaxKind.BoltBracketed> {
export interface BoltSourceFile extends SyntaxBase<SyntaxKind.BoltSourceFile> { export interface BoltSourceFile extends SyntaxBase<SyntaxKind.BoltSourceFile> {
kind: SyntaxKind.BoltSourceFile; kind: SyntaxKind.BoltSourceFile;
elements: BoltSourceElement[]; elements: BoltSourceElement[];
package: Package;
} }
export interface BoltQualName extends SyntaxBase<SyntaxKind.BoltQualName> { export interface BoltQualName extends SyntaxBase<SyntaxKind.BoltQualName> {
@ -740,10 +758,8 @@ export interface BoltParameter extends SyntaxBase<SyntaxKind.BoltParameter> {
} }
export type BoltDeclaration export type BoltDeclaration
= BoltModule = BoltFunctionDeclaration
| BoltFunctionDeclaration
| BoltVariableDeclaration | BoltVariableDeclaration
| BoltImportDeclaration
| BoltTraitDeclaration | BoltTraitDeclaration
| BoltImplDeclaration | BoltImplDeclaration
| BoltTypeAliasDeclaration | BoltTypeAliasDeclaration
@ -757,12 +773,12 @@ export type BoltTypeDeclaration
| BoltRecordDeclaration | BoltRecordDeclaration
export const enum BoltDeclarationModifiers { export const enum BoltModifiers {
Mutable = 1,Public = 2,IsType = 4,IsForeign = 8,} IsMutable = 1,IsPublic = 2,}
export interface BoltModule extends SyntaxBase<SyntaxKind.BoltModule> { export interface BoltModule extends SyntaxBase<SyntaxKind.BoltModule> {
kind: SyntaxKind.BoltModule; kind: SyntaxKind.BoltModule;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
name: BoltQualName; name: BoltQualName;
elements: BoltSourceElement[]; elements: BoltSourceElement[];
} }
@ -779,7 +795,7 @@ export type BoltFunctionBodyElement
export interface BoltFunctionDeclaration extends SyntaxBase<SyntaxKind.BoltFunctionDeclaration> { export interface BoltFunctionDeclaration extends SyntaxBase<SyntaxKind.BoltFunctionDeclaration> {
kind: SyntaxKind.BoltFunctionDeclaration; kind: SyntaxKind.BoltFunctionDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
target: string; target: string;
name: BoltSymbol; name: BoltSymbol;
params: BoltParameter[]; params: BoltParameter[];
@ -790,7 +806,7 @@ export interface BoltFunctionDeclaration extends SyntaxBase<SyntaxKind.BoltFunct
export interface BoltVariableDeclaration extends SyntaxBase<SyntaxKind.BoltVariableDeclaration> { export interface BoltVariableDeclaration extends SyntaxBase<SyntaxKind.BoltVariableDeclaration> {
kind: SyntaxKind.BoltVariableDeclaration; kind: SyntaxKind.BoltVariableDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
bindings: BoltPattern; bindings: BoltPattern;
type: BoltTypeExpression | null; type: BoltTypeExpression | null;
value: BoltExpression | null; value: BoltExpression | null;
@ -805,15 +821,31 @@ export interface BoltPlainImportSymbol extends SyntaxBase<SyntaxKind.BoltPlainIm
name: BoltQualName; name: BoltQualName;
} }
export interface BoltImportDeclaration extends SyntaxBase<SyntaxKind.BoltImportDeclaration> { export interface BoltImportDirective extends SyntaxBase<SyntaxKind.BoltImportDirective> {
kind: SyntaxKind.BoltImportDeclaration; kind: SyntaxKind.BoltImportDirective;
modifiers: BoltModifiers;
file: string; file: string;
symbols: BoltImportSymbol[]; symbols: BoltImportSymbol[];
} }
export interface BoltExportSymbol extends SyntaxBase<SyntaxKind.BoltExportSymbol> {
kind: SyntaxKind.BoltExportSymbol;
}
export interface BoltPlainExportSymbol extends SyntaxBase<SyntaxKind.BoltPlainExportSymbol> {
kind: SyntaxKind.BoltPlainExportSymbol;
name: BoltQualName;
}
export interface BoltExportDirective extends SyntaxBase<SyntaxKind.BoltExportDirective> {
kind: SyntaxKind.BoltExportDirective;
file: string;
symbols: BoltExportSymbol[] | null;
}
export interface BoltTraitDeclaration extends SyntaxBase<SyntaxKind.BoltTraitDeclaration> { export interface BoltTraitDeclaration extends SyntaxBase<SyntaxKind.BoltTraitDeclaration> {
kind: SyntaxKind.BoltTraitDeclaration; kind: SyntaxKind.BoltTraitDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
name: BoltIdentifier; name: BoltIdentifier;
typeParams: BoltTypeParameter[] | null; typeParams: BoltTypeParameter[] | null;
elements: BoltDeclaration[]; elements: BoltDeclaration[];
@ -821,7 +853,7 @@ export interface BoltTraitDeclaration extends SyntaxBase<SyntaxKind.BoltTraitDec
export interface BoltImplDeclaration extends SyntaxBase<SyntaxKind.BoltImplDeclaration> { export interface BoltImplDeclaration extends SyntaxBase<SyntaxKind.BoltImplDeclaration> {
kind: SyntaxKind.BoltImplDeclaration; kind: SyntaxKind.BoltImplDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
name: BoltIdentifier; name: BoltIdentifier;
trait: BoltTypeExpression; trait: BoltTypeExpression;
typeParams: BoltTypeParameter[] | null; typeParams: BoltTypeParameter[] | null;
@ -830,7 +862,7 @@ export interface BoltImplDeclaration extends SyntaxBase<SyntaxKind.BoltImplDecla
export interface BoltTypeAliasDeclaration extends SyntaxBase<SyntaxKind.BoltTypeAliasDeclaration> { export interface BoltTypeAliasDeclaration extends SyntaxBase<SyntaxKind.BoltTypeAliasDeclaration> {
kind: SyntaxKind.BoltTypeAliasDeclaration; kind: SyntaxKind.BoltTypeAliasDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
name: BoltIdentifier; name: BoltIdentifier;
typeParams: BoltTypeParameter[] | null; typeParams: BoltTypeParameter[] | null;
typeExpr: BoltTypeExpression; typeExpr: BoltTypeExpression;
@ -849,7 +881,7 @@ export interface BoltRecordField extends SyntaxBase<SyntaxKind.BoltRecordField>
export interface BoltRecordDeclaration extends SyntaxBase<SyntaxKind.BoltRecordDeclaration> { export interface BoltRecordDeclaration extends SyntaxBase<SyntaxKind.BoltRecordDeclaration> {
kind: SyntaxKind.BoltRecordDeclaration; kind: SyntaxKind.BoltRecordDeclaration;
modifiers: BoltDeclarationModifiers; modifiers: BoltModifiers;
name: BoltIdentifier; name: BoltIdentifier;
typeParms: BoltTypeParameter[] | null; typeParms: BoltTypeParameter[] | null;
members: BoltRecordMember[] | null; members: BoltRecordMember[] | null;
@ -861,10 +893,8 @@ export type BoltSourceElement
| BoltResumeStatement | BoltResumeStatement
| BoltExpressionStatement | BoltExpressionStatement
| BoltMacroCall | BoltMacroCall
| BoltModule
| BoltFunctionDeclaration | BoltFunctionDeclaration
| BoltVariableDeclaration | BoltVariableDeclaration
| BoltImportDeclaration
| BoltTraitDeclaration | BoltTraitDeclaration
| BoltImplDeclaration | BoltImplDeclaration
| BoltTypeAliasDeclaration | BoltTypeAliasDeclaration
@ -873,6 +903,9 @@ export type BoltSourceElement
| BoltTraitDeclaration | BoltTraitDeclaration
| BoltTypeAliasDeclaration | BoltTypeAliasDeclaration
| BoltRecordDeclaration | BoltRecordDeclaration
| BoltModule
| BoltImportDirective
| BoltExportDirective
export interface BoltMacroCall extends SyntaxBase<SyntaxKind.BoltMacroCall> { export interface BoltMacroCall extends SyntaxBase<SyntaxKind.BoltMacroCall> {
@ -1314,6 +1347,7 @@ export type BoltSyntax
| BoltYieldKeyword | BoltYieldKeyword
| BoltMatchKeyword | BoltMatchKeyword
| BoltImportKeyword | BoltImportKeyword
| BoltExportKeyword
| BoltPubKeyword | BoltPubKeyword
| BoltModKeyword | BoltModKeyword
| BoltMutKeyword | BoltMutKeyword
@ -1359,7 +1393,10 @@ export type BoltSyntax
| BoltFunctionDeclaration | BoltFunctionDeclaration
| BoltVariableDeclaration | BoltVariableDeclaration
| BoltPlainImportSymbol | BoltPlainImportSymbol
| BoltImportDeclaration | BoltImportDirective
| BoltExportSymbol
| BoltPlainExportSymbol
| BoltExportDirective
| BoltTraitDeclaration | BoltTraitDeclaration
| BoltImplDeclaration | BoltImplDeclaration
| BoltTypeAliasDeclaration | BoltTypeAliasDeclaration
@ -1467,6 +1504,7 @@ export type Syntax
| BoltYieldKeyword | BoltYieldKeyword
| BoltMatchKeyword | BoltMatchKeyword
| BoltImportKeyword | BoltImportKeyword
| BoltExportKeyword
| BoltPubKeyword | BoltPubKeyword
| BoltModKeyword | BoltModKeyword
| BoltMutKeyword | BoltMutKeyword
@ -1512,7 +1550,10 @@ export type Syntax
| BoltFunctionDeclaration | BoltFunctionDeclaration
| BoltVariableDeclaration | BoltVariableDeclaration
| BoltPlainImportSymbol | BoltPlainImportSymbol
| BoltImportDeclaration | BoltImportDirective
| BoltExportSymbol
| BoltPlainExportSymbol
| BoltExportDirective
| BoltTraitDeclaration | BoltTraitDeclaration
| BoltImplDeclaration | BoltImplDeclaration
| BoltTypeAliasDeclaration | BoltTypeAliasDeclaration
@ -1618,6 +1659,7 @@ export function createBoltLoopKeyword(span?: TextSpan | null): BoltLoopKeyword;
export function createBoltYieldKeyword(span?: TextSpan | null): BoltYieldKeyword; export function createBoltYieldKeyword(span?: TextSpan | null): BoltYieldKeyword;
export function createBoltMatchKeyword(span?: TextSpan | null): BoltMatchKeyword; export function createBoltMatchKeyword(span?: TextSpan | null): BoltMatchKeyword;
export function createBoltImportKeyword(span?: TextSpan | null): BoltImportKeyword; export function createBoltImportKeyword(span?: TextSpan | null): BoltImportKeyword;
export function createBoltExportKeyword(span?: TextSpan | null): BoltExportKeyword;
export function createBoltPubKeyword(span?: TextSpan | null): BoltPubKeyword; export function createBoltPubKeyword(span?: TextSpan | null): BoltPubKeyword;
export function createBoltModKeyword(span?: TextSpan | null): BoltModKeyword; export function createBoltModKeyword(span?: TextSpan | null): BoltModKeyword;
export function createBoltMutKeyword(span?: TextSpan | null): BoltMutKeyword; export function createBoltMutKeyword(span?: TextSpan | null): BoltMutKeyword;
@ -1629,7 +1671,7 @@ export function createBoltImplKeyword(span?: TextSpan | null): BoltImplKeyword;
export function createBoltParenthesized(text: string, span?: TextSpan | null): BoltParenthesized; export function createBoltParenthesized(text: string, span?: TextSpan | null): BoltParenthesized;
export function createBoltBraced(text: string, span?: TextSpan | null): BoltBraced; export function createBoltBraced(text: string, span?: TextSpan | null): BoltBraced;
export function createBoltBracketed(text: string, span?: TextSpan | null): BoltBracketed; export function createBoltBracketed(text: string, span?: TextSpan | null): BoltBracketed;
export function createBoltSourceFile(elements: BoltSourceElement[], span?: TextSpan | null): BoltSourceFile; export function createBoltSourceFile(elements: BoltSourceElement[], package: Package, span?: TextSpan | null): BoltSourceFile;
export function createBoltQualName(modulePath: BoltIdentifier[] | null, name: BoltSymbol, span?: TextSpan | null): BoltQualName; export function createBoltQualName(modulePath: BoltIdentifier[] | null, name: BoltSymbol, span?: TextSpan | null): BoltQualName;
export function createBoltReferenceTypeExpression(name: BoltQualName, arguments: BoltTypeExpression[] | null, span?: TextSpan | null): BoltReferenceTypeExpression; export function createBoltReferenceTypeExpression(name: BoltQualName, arguments: BoltTypeExpression[] | null, span?: TextSpan | null): BoltReferenceTypeExpression;
export function createBoltFunctionTypeExpression(params: BoltParameter[], returnType: BoltTypeExpression | null, span?: TextSpan | null): BoltFunctionTypeExpression; export function createBoltFunctionTypeExpression(params: BoltParameter[], returnType: BoltTypeExpression | null, span?: TextSpan | null): BoltFunctionTypeExpression;
@ -1659,16 +1701,19 @@ export function createBoltConditionalStatement(cases: BoltConditionalCase[], spa
export function createBoltResumeStatement(value: BoltExpression, span?: TextSpan | null): BoltResumeStatement; export function createBoltResumeStatement(value: BoltExpression, span?: TextSpan | null): BoltResumeStatement;
export function createBoltExpressionStatement(expression: BoltExpression, span?: TextSpan | null): BoltExpressionStatement; export function createBoltExpressionStatement(expression: BoltExpression, span?: TextSpan | null): BoltExpressionStatement;
export function createBoltParameter(index: number, bindings: BoltPattern, type: BoltTypeExpression | null, defaultValue: BoltExpression | null, span?: TextSpan | null): BoltParameter; export function createBoltParameter(index: number, bindings: BoltPattern, type: BoltTypeExpression | null, defaultValue: BoltExpression | null, span?: TextSpan | null): BoltParameter;
export function createBoltModule(modifiers: BoltDeclarationModifiers, name: BoltQualName, elements: BoltSourceElement[], span?: TextSpan | null): BoltModule; export function createBoltModule(modifiers: BoltModifiers, name: BoltQualName, elements: BoltSourceElement[], span?: TextSpan | null): BoltModule;
export function createBoltFunctionDeclaration(modifiers: BoltDeclarationModifiers, target: string, name: BoltSymbol, params: BoltParameter[], returnType: BoltTypeExpression | null, typeParams: BoltTypeParameter[] | null, body: BoltFunctionBodyElement[], span?: TextSpan | null): BoltFunctionDeclaration; export function createBoltFunctionDeclaration(modifiers: BoltModifiers, target: string, name: BoltSymbol, params: BoltParameter[], returnType: BoltTypeExpression | null, typeParams: BoltTypeParameter[] | null, body: BoltFunctionBodyElement[], span?: TextSpan | null): BoltFunctionDeclaration;
export function createBoltVariableDeclaration(modifiers: BoltDeclarationModifiers, bindings: BoltPattern, type: BoltTypeExpression | null, value: BoltExpression | null, span?: TextSpan | null): BoltVariableDeclaration; export function createBoltVariableDeclaration(modifiers: BoltModifiers, bindings: BoltPattern, type: BoltTypeExpression | null, value: BoltExpression | null, span?: TextSpan | null): BoltVariableDeclaration;
export function createBoltPlainImportSymbol(name: BoltQualName, span?: TextSpan | null): BoltPlainImportSymbol; export function createBoltPlainImportSymbol(name: BoltQualName, span?: TextSpan | null): BoltPlainImportSymbol;
export function createBoltImportDeclaration(file: string, symbols: BoltImportSymbol[], span?: TextSpan | null): BoltImportDeclaration; export function createBoltImportDirective(modifiers: BoltModifiers, file: string, symbols: BoltImportSymbol[], span?: TextSpan | null): BoltImportDirective;
export function createBoltTraitDeclaration(modifiers: BoltDeclarationModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltTraitDeclaration; export function createBoltExportSymbol(span?: TextSpan | null): BoltExportSymbol;
export function createBoltImplDeclaration(modifiers: BoltDeclarationModifiers, name: BoltIdentifier, trait: BoltTypeExpression, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltImplDeclaration; export function createBoltPlainExportSymbol(name: BoltQualName, span?: TextSpan | null): BoltPlainExportSymbol;
export function createBoltTypeAliasDeclaration(modifiers: BoltDeclarationModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, typeExpr: BoltTypeExpression, span?: TextSpan | null): BoltTypeAliasDeclaration; export function createBoltExportDirective(file: string, symbols: BoltExportSymbol[] | null, span?: TextSpan | null): BoltExportDirective;
export function createBoltTraitDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltTraitDeclaration;
export function createBoltImplDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, trait: BoltTypeExpression, typeParams: BoltTypeParameter[] | null, elements: BoltDeclaration[], span?: TextSpan | null): BoltImplDeclaration;
export function createBoltTypeAliasDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParams: BoltTypeParameter[] | null, typeExpr: BoltTypeExpression, span?: TextSpan | null): BoltTypeAliasDeclaration;
export function createBoltRecordField(name: BoltIdentifier, type: BoltTypeExpression, span?: TextSpan | null): BoltRecordField; export function createBoltRecordField(name: BoltIdentifier, type: BoltTypeExpression, span?: TextSpan | null): BoltRecordField;
export function createBoltRecordDeclaration(modifiers: BoltDeclarationModifiers, name: BoltIdentifier, typeParms: BoltTypeParameter[] | null, members: BoltRecordMember[] | null, span?: TextSpan | null): BoltRecordDeclaration; export function createBoltRecordDeclaration(modifiers: BoltModifiers, name: BoltIdentifier, typeParms: BoltTypeParameter[] | null, members: BoltRecordMember[] | null, span?: TextSpan | null): BoltRecordDeclaration;
export function createBoltMacroCall(name: BoltIdentifier, text: string, span?: TextSpan | null): BoltMacroCall; export function createBoltMacroCall(name: BoltIdentifier, text: string, span?: TextSpan | null): BoltMacroCall;
export function createJSOperator(text: string, span?: TextSpan | null): JSOperator; export function createJSOperator(text: string, span?: TextSpan | null): JSOperator;
export function createJSIdentifier(text: string, span?: TextSpan | null): JSIdentifier; export function createJSIdentifier(text: string, span?: TextSpan | null): JSIdentifier;
@ -1772,6 +1817,7 @@ export function isBoltLoopKeyword(value: any): value is BoltLoopKeyword;
export function isBoltYieldKeyword(value: any): value is BoltYieldKeyword; export function isBoltYieldKeyword(value: any): value is BoltYieldKeyword;
export function isBoltMatchKeyword(value: any): value is BoltMatchKeyword; export function isBoltMatchKeyword(value: any): value is BoltMatchKeyword;
export function isBoltImportKeyword(value: any): value is BoltImportKeyword; export function isBoltImportKeyword(value: any): value is BoltImportKeyword;
export function isBoltExportKeyword(value: any): value is BoltExportKeyword;
export function isBoltPubKeyword(value: any): value is BoltPubKeyword; export function isBoltPubKeyword(value: any): value is BoltPubKeyword;
export function isBoltModKeyword(value: any): value is BoltModKeyword; export function isBoltModKeyword(value: any): value is BoltModKeyword;
export function isBoltMutKeyword(value: any): value is BoltMutKeyword; export function isBoltMutKeyword(value: any): value is BoltMutKeyword;
@ -1826,7 +1872,10 @@ export function isBoltFunctionDeclaration(value: any): value is BoltFunctionDecl
export function isBoltVariableDeclaration(value: any): value is BoltVariableDeclaration; export function isBoltVariableDeclaration(value: any): value is BoltVariableDeclaration;
export function isBoltImportSymbol(value: any): value is BoltImportSymbol; export function isBoltImportSymbol(value: any): value is BoltImportSymbol;
export function isBoltPlainImportSymbol(value: any): value is BoltPlainImportSymbol; export function isBoltPlainImportSymbol(value: any): value is BoltPlainImportSymbol;
export function isBoltImportDeclaration(value: any): value is BoltImportDeclaration; export function isBoltImportDirective(value: any): value is BoltImportDirective;
export function isBoltExportSymbol(value: any): value is BoltExportSymbol;
export function isBoltPlainExportSymbol(value: any): value is BoltPlainExportSymbol;
export function isBoltExportDirective(value: any): value is BoltExportDirective;
export function isBoltTraitDeclaration(value: any): value is BoltTraitDeclaration; export function isBoltTraitDeclaration(value: any): value is BoltTraitDeclaration;
export function isBoltImplDeclaration(value: any): value is BoltImplDeclaration; export function isBoltImplDeclaration(value: any): value is BoltImplDeclaration;
export function isBoltTypeAliasDeclaration(value: any): value is BoltTypeAliasDeclaration; export function isBoltTypeAliasDeclaration(value: any): value is BoltTypeAliasDeclaration;

View file

@ -7,11 +7,17 @@ import { sync as globSync } from "glob"
import * as path from "path" import * as path from "path"
import * as fs from "fs" import * as fs from "fs"
import yargs from "yargs" import yargs from "yargs"
import yaml from "js-yaml"
import semver from "semver"
import { Program } from "../program" import { Program } from "../program"
import { parseSourceFile } from "../parser" import { parseSourceFile } from "../parser"
import { BoltSourceFile} from "../ast"
import { Frontend } from "../frontend" import { Frontend } from "../frontend"
import { Package } from "../common"
import {hasOwnProperty} from "../util"
import {isString} from "util"
import {DiagnosticPrinter, E_FIELD_NOT_PRESENT, E_FIELD_MUST_BE_STRING, E_FIELD_HAS_INVALID_VERSION_NUMBER} from "../diagnostics"
import {BoltSourceFileModifiers} from "../ast"
//global.print = function (value: any) { //global.print = function (value: any) {
// console.error(require('util').inspect(value, { depth: Infinity, colors: true })) // console.error(require('util').inspect(value, { depth: Infinity, colors: true }))
@ -43,16 +49,99 @@ function flatMap<T>(array: T[], proc: (element: T) => T[]) {
return out return out
} }
function loadAllSourceFiles(filenames: string[]): BoltSourceFile[] { const diagnostics = new DiagnosticPrinter();
const sourceFiles = [];
function loadPackageMetadata(rootDir: string) {
let name = null
let version = null;
let hasVersionErrors = false;
let hasNameErrors = false;
const filepath = path.join(rootDir, 'Boltfile');
if (fs.existsSync(filepath)) {
const data = yaml.safeLoad(fs.readFileSync(filepath, 'utf8'));
if (data !== undefined) {
if (hasOwnProperty(data, 'name')) {
if (!isString(data.name)) {
diagnostics.add({
message: E_FIELD_MUST_BE_STRING,
severity: 'error',
args: { name: 'name' },
});
hasNameErrors = true;
} else {
name = data.name;
}
}
if (hasOwnProperty(data, 'version')) {
if (!isString(data.version)) {
diagnostics.add({
message: E_FIELD_MUST_BE_STRING,
args: { name: 'version' },
severity: 'error',
});
hasVersionErrors = true;
} else {
if (!semver.valid(data.version)) {
diagnostics.add({
message: E_FIELD_HAS_INVALID_VERSION_NUMBER,
args: { name: 'version' },
severity: 'error',
});
hasVersionErrors = true;
} else {
version = data.version;
}
}
}
}
}
if (name === null && !hasNameErrors) {
diagnostics.add({
message: E_FIELD_NOT_PRESENT,
severity: 'warning',
args: { name: 'name' },
});
}
if (version === null && !hasVersionErrors) {
diagnostics.add({
message: E_FIELD_NOT_PRESENT,
severity: 'warning',
args: { name: 'version' },
});
}
return {
name,
version
};
}
function loadPackage(rootDir: string): Package {
const data = loadPackageMetadata(rootDir);
const pkg = new Package(rootDir, data.name, data.version, []);
for (const filepath of globSync(path.join(rootDir, '**/*.bolt'))) {
pkg.addSourceFile(parseSourceFile(filepath, pkg));
}
return pkg;
}
function loadPackagesAndSourceFiles(filenames: string[], cwd = '.'): Package[] {
const anonPkg = new Package(cwd, null, null, []);
const pkgs = [ anonPkg ];
for (const filename of filenames) { for (const filename of filenames) {
if (fs.statSync(filename).isDirectory()) { if (fs.statSync(filename).isDirectory()) {
for (const filepath of globSync(path.join(filename, '**/*.bolt'))) { pkgs.push(loadPackage(filename));
sourceFiles.push(parseSourceFile(filepath)); } else {
anonPkg.addSourceFile(parseSourceFile(filename, anonPkg, 0));
} }
} }
} return pkgs;
return sourceFiles;
} }
yargs yargs
@ -90,8 +179,8 @@ yargs
, args => { , args => {
const sourceFiles = loadAllSourceFiles(toArray(args.files as string[] | string)); const pkgs = loadPackagesAndSourceFiles(toArray(args.files as string[] | string));
const program = new Program(sourceFiles); const program = new Program(pkgs);
const frontend = new Frontend(); const frontend = new Frontend();
frontend.typeCheck(program); frontend.typeCheck(program);
if (frontend.diagnostics.hasErrors && !args.force) { if (frontend.diagnostics.hasErrors && !args.force) {

View file

@ -24,13 +24,13 @@
*/ */
import { import {
isSyntax,
Syntax, Syntax,
SyntaxKind, SyntaxKind,
BoltReferenceExpression, BoltReferenceExpression,
BoltDeclaration, BoltDeclaration,
BoltSourceFile, BoltSourceFile,
BoltSyntax, BoltSyntax,
BoltReferenceTypeExpression,
BoltTypeDeclaration, BoltTypeDeclaration,
BoltExpression, BoltExpression,
BoltFunctionDeclaration, BoltFunctionDeclaration,
@ -40,9 +40,11 @@ import {
BoltTypeExpression, BoltTypeExpression,
BoltSourceElement, BoltSourceElement,
isBoltStatement, isBoltStatement,
isBoltDeclaration isBoltDeclaration,
isSourceFile,
BoltReferenceTypeExpression
} from "./ast"; } from "./ast";
import {FastStringMap, memoize, assert} from "./util"; import {FastStringMap, memoize, assert, verbose} from "./util";
import { import {
DiagnosticPrinter, DiagnosticPrinter,
E_TYPES_NOT_ASSIGNABLE, E_TYPES_NOT_ASSIGNABLE,
@ -53,15 +55,13 @@ import {
E_INVALID_ARGUMENTS E_INVALID_ARGUMENTS
} from "./diagnostics"; } from "./diagnostics";
import { createAnyType, isOpaqueType, createOpaqueType, Type, createVoidType, createVariantType, isVoidType } from "./types"; import { createAnyType, isOpaqueType, createOpaqueType, Type, createVoidType, createVariantType, isVoidType } from "./types";
import { getReturnStatementsInFunctionBody } from "./common"; import { getReturnStatementsInFunctionBody, isAutoImported, toDeclarationPath, createDeclarationPath, hasRelativeModulePath, hasAbsoluteModulePath, DeclarationPath, getModulePath, getSymbolNameOfDeclarationPath } from "./common";
import {emit} from "./emitter"; import {emit} from "./emitter";
interface SymbolInfo { const PACKAGE_SCOPE_ID = 0;
declarations: BoltDeclaration[];
}
interface TypeSymbolInfo { interface SymbolInfo<N extends Syntax> {
declarations: BoltTypeDeclaration[]; declarations: N[];
} }
function introducesNewScope(kind: SyntaxKind): boolean { function introducesNewScope(kind: SyntaxKind): boolean {
@ -79,12 +79,120 @@ function introducesNewTypeScope(kind: SyntaxKind): boolean {
type Scope = unknown; type Scope = unknown;
type TypeScope = unknown; type TypeScope = unknown;
function getScopeId(scope: Scope | TypeScope) {
if (isSyntax(scope)) {
return scope.id;
}
return PACKAGE_SCOPE_ID;
}
function createSymbol(node: BoltDeclaration): SymbolInfo { function createSymbol(node: BoltDeclaration): SymbolInfo {
return { declarations: [ node ] }; return { declarations: [ node ] };
} }
function createTypeSymbol(node: BoltTypeDeclaration): TypeSymbolInfo { class SymbolResolver<N extends Syntax> {
return { declarations: [ node ] };
private symbols = new FastStringMap<string, SymbolInfo<N>>();
constructor(private introducesNewScope: (kind: SyntaxKind) => boolean) {
}
public addSymbol(name: string, node: N): void {
const scope = this.getScopeSurroundingNode(node)
verbose(`Adding symbol ${name} in scope #${getScopeId(scope)}`);
const sym = { declarations: [ node ] };
this.symbols.set(`${name}@${getScopeId(scope)}`, sym);
}
public getParentScope(scope: Scope): Scope | null {
if (!isSyntax(scope)) {
// Scope is the global package scope
return null;
}
if (scope.kind === SyntaxKind.BoltSourceFile) {
return scope.package;
}
return this.getScopeForNode(scope.parentNode!)
}
public getScopeSurroundingNode(node: Syntax): Scope {
assert(node.parentNode !== null);
return this.getScopeForNode(node.parentNode!);
}
public getScopeForNode(node: Syntax): Scope {
let currNode = node;
while (!this.introducesNewScope(currNode.kind)) {
if (currNode.kind === SyntaxKind.BoltSourceFile) {
return currNode.package;
}
currNode = currNode.parentNode!;
}
return currNode;
}
private lookupSymbolInScope(name: string, scope: Scope): SymbolInfo<N> | null {
const key = `${name}@${getScopeId(scope)}`;
if (!this.symbols.has(key)) {
return null;
}
return this.symbols.get(key);
}
public findSymbolInScopeOf(name: string, scope: Scope): SymbolInfo<N> | null {
while (true) {
const sym = this.lookupSymbolInScope(name, scope);
if (sym !== null) {
return sym;
}
const parentScope = this.getParentScope(scope);
if (parentScope === null) {
break;
}
scope = parentScope;
}
return null;
}
public resolve(path: DeclarationPath, node: BoltSyntax): BoltSyntax | null {
let scope = this.getScopeSurroundingNode(node);
if (hasAbsoluteModulePath(path)) {
// TODO
} else if (hasRelativeModulePath(path)) {
while (true) {
let shouldSearchParentScopes = false;
let currScope = scope;
for (const name of getModulePath(path)) {
const sym = this.lookupSymbolInScope(name, currScope);
if (sym === null) {
shouldSearchParentScopes = true;
break;
}
if (sym.declarations[0].kind !== SyntaxKind.BoltModule) {
shouldSearchParentScopes = true;
break;
}
currScope = this.getScopeForNode(sym.declarations[0]);
}
if (!shouldSearchParentScopes) {
scope = currScope;
break;
}
const parentScope = this.getParentScope(scope);
if (parentScope === null) {
return null;
}
scope = parentScope;
}
}
const sym = this.findSymbolInScopeOf(getSymbolNameOfDeclarationPath(path), scope);
if (sym === null) {
return null;
}
return sym.declarations[0]!;
}
} }
export class TypeChecker { export class TypeChecker {
@ -93,8 +201,8 @@ export class TypeChecker {
} }
private symbols = new FastStringMap<string, SymbolInfo>(); private varResolver = new SymbolResolver<BoltDeclaration>(introducesNewScope);
private typeSymbols = new FastStringMap<string, TypeSymbolInfo>(); private typeResolver = new SymbolResolver<BoltTypeDeclaration>(introducesNewTypeScope);
public checkSourceFile(node: BoltSourceFile): void { public checkSourceFile(node: BoltSourceFile): void {
@ -107,12 +215,15 @@ export class TypeChecker {
switch (node.kind) { switch (node.kind) {
case SyntaxKind.BoltConstantExpression:
break;
case SyntaxKind.BoltReferenceExpression: case SyntaxKind.BoltReferenceExpression:
{ {
if (self.resolveReferenceExpression(node) === null) { if (self.resolveReferenceExpression(node) === null) {
self.diagnostics.add({ self.diagnostics.add({
message: E_DECLARATION_NOT_FOUND, message: E_DECLARATION_NOT_FOUND,
args: { name: node.name.name.text }, args: { name: emit(node.name.name) },
severity: 'error', severity: 'error',
node: node, node: node,
}) })
@ -186,7 +297,7 @@ export class TypeChecker {
if (self.resolveTypeReferenceExpression(node) === null) { if (self.resolveTypeReferenceExpression(node) === null) {
self.diagnostics.add({ self.diagnostics.add({
message: E_TYPE_DECLARATION_NOT_FOUND, message: E_TYPE_DECLARATION_NOT_FOUND,
args: { name: node.name.name.text }, args: { name: emit(node.name.name) },
severity: 'error', severity: 'error',
node: node, node: node,
}) })
@ -338,15 +449,19 @@ export class TypeChecker {
visitStatement(node); visitStatement(node);
} else if (isBoltDeclaration(node)) { } else if (isBoltDeclaration(node)) {
visitDeclaration(node); visitDeclaration(node);
} else { } else if (node.kind === SyntaxKind.BoltModule) {
throw new Error(`Unknown node of kind ${kindToString(node)}`); for (const element of node.elements) {
visitSourceElement(element);
}
} else if (node.kind !== SyntaxKind.BoltImportDirective) {
throw new Error(`Unknown node of kind ${kindToString(node.kind)}`);
} }
} }
} }
private resolveType(name: string, node: BoltSyntax): Type | null { private resolveType(name: string, node: BoltSyntax): Type | null {
const sym = this.findSymbolInTypeScopeOf(name, this.getTypeScopeSurroundingNode(node)) const sym = this.typeResolver.findSymbolInScopeOf(name, this.typeResolver.getScopeSurroundingNode(node))
if (sym === null) { if (sym === null) {
return null; return null;
} }
@ -429,11 +544,13 @@ export class TypeChecker {
function visitExpression(node: BoltExpression) { function visitExpression(node: BoltExpression) {
switch (node.kind) { switch (node.kind) {
case SyntaxKind.BoltReferenceExpression: case SyntaxKind.BoltReferenceExpression:
{
const resolved = self.resolveReferenceExpression(node); const resolved = self.resolveReferenceExpression(node);
if (resolved !== null) { if (resolved !== null) {
visitFunctionBodyElement(resolved); visitFunctionBodyElement(resolved);
} }
break; break;
}
default: default:
throw new Error(`Unexpected node type ${kindToString(node.kind)}`); throw new Error(`Unexpected node type ${kindToString(node.kind)}`);
} }
@ -475,182 +592,25 @@ export class TypeChecker {
case SyntaxKind.BoltFunctionDeclaration: case SyntaxKind.BoltFunctionDeclaration:
{ {
const scope = this.getScopeSurroundingNode(node); this.varResolver.addSymbol(emit(node.name), node);
const sym = createSymbol(node);
this.addSymbol(emit(node.name), scope, sym);
break; break;
} }
case SyntaxKind.BoltRecordDeclaration: case SyntaxKind.BoltRecordDeclaration:
{ {
const typeScope = this.getTypeScopeSurroundingNode(node); this.typeResolver.addSymbol(node.name.text, node);
const typeSym = createTypeSymbol(node);
this.addTypeSymbol(node.name.text, typeScope, typeSym);
} }
} }
} }
private addSymbol(name: string, scope: Scope, sym: SymbolInfo): void { private resolveReferenceExpression(node: BoltReferenceExpression): BoltDeclaration | null {
console.error(`Adding symbol ${name}`); return this.varResolver.resolve(createDeclarationPath(node.name), node);
this.symbols.set(`${name}@${(scope as any).id}`, sym);
} }
private addTypeSymbol(name: string, scope: TypeScope, sym: TypeSymbolInfo): void { private resolveTypeReferenceExpression(node: BoltReferenceTypeExpression): BoltTypeDeclaration | null {
console.error(`Adding type symbol ${name}`); return this.typeResolver.resolve(createDeclarationPath(node.name), node);
this.typeSymbols.set(`${name}@${(scope as any).id}`, sym);
}
public getParentScope(scope: Scope): Scope | null {
let node = scope as Syntax;
if (node.kind === SyntaxKind.BoltSourceFile) {
return null;
}
node = node.parentNode!;
while (!introducesNewScope(node.kind)) {
node = node.parentNode!;
}
return node;
}
public getParentTypeScope(scope: TypeScope): TypeScope | null {
let node = scope as Syntax;
if (node.kind === SyntaxKind.BoltSourceFile) {
return null;
}
node = node.parentNode!;
while (!introducesNewTypeScope(node.kind)) {
node = node.parentNode!;
}
return node;
}
private getScopeSurroundingNode(node: Syntax): Scope {
if (node.kind === SyntaxKind.BoltSourceFile) {
return node;
}
return this.getScopeForNode(node.parentNode);
}
private getTypeScopeSurroundingNode(node: Syntax): TypeScope {
if (node.kind === SyntaxKind.BoltSourceFile) {
return node;
}
return this.getScopeForNode(node.parentNode);
}
private getScopeForNode(node: Syntax): Scope {
if (node.kind === SyntaxKind.BoltSourceFile) {
return node;
}
let currNode = node;
while (!introducesNewScope(currNode.kind)) {
currNode = currNode.parentNode!;
}
return currNode;
}
private getTypeScopeForNode(node: Syntax): TypeScope {
if (node.kind === SyntaxKind.BoltSourceFile) {
return node;
}
let currNode = node;
while (!introducesNewTypeScope(currNode.kind)) {
currNode = currNode.parentNode!;
}
return currNode;
}
private lookupSymbolInScope(name: string, scope: Scope): SymbolInfo | null {
const key = `${name}@${(scope as any).id}`;
if (!this.symbols.has(key)) {
return null;
}
return this.symbols.get(key);
}
private lookupSymbolInTypeScope(name: string, scope: TypeScope): TypeSymbolInfo | null {
const key = `${name}@${(scope as any).id}`;
if (!this.typeSymbols.has(key)) {
return null;
}
return this.typeSymbols.get(key);
}
public findSymbolInScopeOf(name: string, scope: Scope): SymbolInfo | null {
while (true) {
const sym = this.lookupSymbolInScope(name, scope);
if (sym !== null) {
return sym;
}
const parentScope = this.getParentScope(scope);
if (parentScope === null) {
break;
}
scope = parentScope;
}
return null;
}
public findSymbolInTypeScopeOf(name: string, scope: TypeScope): TypeSymbolInfo | null {
while (true) {
const sym = this.lookupSymbolInTypeScope(name, scope);
if (sym !== null) {
return sym;
}
const parentTypeScope = this.getParentTypeScope(scope);
if (parentTypeScope === null) {
break;
}
scope = parentTypeScope;
}
return null;
}
public resolveReferenceExpression(node: BoltReferenceExpression): BoltDeclaration | null {
let scope = this.getScopeSurroundingNode(node);
if (node.name.modulePath !== null) {
while (true) {
let shouldSearchParentScopes = false;
let currScope = scope;
for (const name of node.name.modulePath) {
const sym = this.lookupSymbolInScope(name.text, currScope);
if (sym === null) {
shouldSearchParentScopes = true;
break;
}
if (sym.declarations[0].kind !== SyntaxKind.BoltModule) {
shouldSearchParentScopes = true;
break;
}
currScope = this.getScopeForNode(sym.declarations[0]);
}
if (!shouldSearchParentScopes) {
scope = currScope;
break;
}
const parentScope = this.getParentScope(scope);
if (parentScope === null) {
return null;
}
scope = parentScope;
}
}
const sym = this.findSymbolInScopeOf(emit(node.name.name), scope);
if (sym === null) {
return null;
}
return sym.declarations[0]!;
}
public resolveTypeReferenceExpression(node: BoltReferenceTypeExpression): BoltTypeDeclaration | null {
const typeScope = this.getTypeScopeSurroundingNode(node);
const typeSym = this.findSymbolInTypeScopeOf(emit(node.name.name), typeScope);
if (typeSym === null) {
return null;
}
return typeSym.declarations[0]!;
} }
} }

View file

@ -7,7 +7,10 @@ import {
kindToString, kindToString,
Syntax, Syntax,
Token, Token,
isBoltPunctuated isBoltPunctuated,
SourceFile,
BoltSourceFile,
BoltSourceFileModifiers
} from "./ast"; } from "./ast";
import { BOLT_SUPPORTED_LANGUAGES } from "./constants" import { BOLT_SUPPORTED_LANGUAGES } from "./constants"
import {emit} from "./emitter"; import {emit} from "./emitter";
@ -15,6 +18,30 @@ import {FastStringMap, enumerate, escapeChar} from "./util";
import {TextSpan, TextPos, TextFile} from "./text"; import {TextSpan, TextPos, TextFile} from "./text";
import {Scanner} from "./scanner"; import {Scanner} from "./scanner";
export class Package {
constructor(
public rootDir: string,
public name: string | null,
public version: string | null,
public sourceFiles: SourceFile[],
) {
}
public addSourceFile(sourceFile: SourceFile) {
this.sourceFiles.push(sourceFile);
}
}
export function isAutoImported(node: BoltSourceFile): boolean {
//if (node.kind !== SyntaxKind.BoltSourceFile) {
// node = node.getParentOfKind(SyntaxKind.BoltSourceFile)!
//}
return (node.modifiers & BoltSourceFileModifiers.AutoImport) > 0;
}
export function getLanguage(node: Syntax): string { export function getLanguage(node: Syntax): string {
const kindStr = kindToString(node.kind); const kindStr = kindToString(node.kind);
for (const prefix of BOLT_SUPPORTED_LANGUAGES) { for (const prefix of BOLT_SUPPORTED_LANGUAGES) {
@ -164,6 +191,10 @@ export class OperatorTable {
export function describeKind(kind: SyntaxKind): string { export function describeKind(kind: SyntaxKind): string {
switch (kind) { switch (kind) {
case SyntaxKind.BoltImportKeyword:
return "'import'";
case SyntaxKind.BoltExportKeyword:
return "'export'";
case SyntaxKind.BoltExMark: case SyntaxKind.BoltExMark:
return "'!'"; return "'!'";
case SyntaxKind.JSIdentifier: case SyntaxKind.JSIdentifier:
@ -257,7 +288,7 @@ export function describeKind(kind: SyntaxKind): string {
case SyntaxKind.BoltTraitKeyword: case SyntaxKind.BoltTraitKeyword:
return "'impl'"; return "'impl'";
case SyntaxKind.BoltImplKeyword: case SyntaxKind.BoltImplKeyword:
return "'trait'"; return "'impl'";
case SyntaxKind.BoltForKeyword: case SyntaxKind.BoltForKeyword:
return "'for'"; return "'for'";
case SyntaxKind.JSMulOp: case SyntaxKind.JSMulOp:
@ -297,11 +328,37 @@ export function describeKind(kind: SyntaxKind): string {
} }
} }
export function toDeclarationPath(node: BoltQualName): string[] { export type DeclarationPath = unknown;
const lastElement = emit(node.name);
if (node.modulePath === null) { type DeclarationPathInfo = {
return [ lastElement ]; modulePath: string[],
} isAbsolute: boolean,
return [...node.modulePath.map(id => id.text), lastElement]; name: string
};
export function getModulePath(path: DeclarationPath): string[] {
return (path as DeclarationPathInfo).modulePath;
}
export function hasAbsoluteModulePath(path: DeclarationPath): boolean {
return (path as DeclarationPathInfo).modulePath.length > 0
&& (path as DeclarationPathInfo).isAbsolute;
}
export function hasRelativeModulePath(path: DeclarationPath): boolean {
return (path as DeclarationPathInfo).modulePath.length > 0
&& !(path as DeclarationPathInfo).isAbsolute;
}
export function getSymbolNameOfDeclarationPath(path: DeclarationPath): string {
return (path as DeclarationPathInfo).name;
}
export function createDeclarationPath(node: BoltQualName): DeclarationPath {
const name = emit(node.name);
if (node.modulePath === null) {
return { modulePath: [], isAbsolute: false, name };
}
return { modulePath: node.modulePath.map(id => id.text), isAbsolute: false, name };
} }

View file

@ -3,6 +3,9 @@ import chalk from "chalk"
import {Syntax} from "./ast"; import {Syntax} from "./ast";
import {format, MapLike, FormatArg} from "./util"; import {format, MapLike, FormatArg} from "./util";
export const E_FIELD_HAS_INVALID_VERSION_NUMBER = "Field '{name}' contains an invalid version nunmber."
export const E_FIELD_MUST_BE_STRING = "Field '{name}' must be a string."
export const E_FIELD_NOT_PRESENT = "Field '{name}' is not present."
export const E_TYPE_DECLARATION_NOT_FOUND = "A type declaration named '{name}' was not found." export const E_TYPE_DECLARATION_NOT_FOUND = "A type declaration named '{name}' was not found."
export const E_DECLARATION_NOT_FOUND = "Reference to an undefined declaration '{name}'."; export const E_DECLARATION_NOT_FOUND = "Reference to an undefined declaration '{name}'.";
export const E_TYPES_NOT_ASSIGNABLE = "Types {left} and {right} are not assignable."; export const E_TYPES_NOT_ASSIGNABLE = "Types {left} and {right} are not assignable.";
@ -84,6 +87,9 @@ export class DiagnosticPrinter {
case 'error': case 'error':
this.hasErrors = true; this.hasErrors = true;
out += chalk.bold.red('error: '); out += chalk.bold.red('error: ');
case 'warning':
this.hasErrors = true;
out += chalk.bold.red('warning: ');
} }
if (diagnostic.args !== undefined) { if (diagnostic.args !== undefined) {
out += format(diagnostic.message, diagnostic.args) + '\n'; out += format(diagnostic.message, diagnostic.args) + '\n';

View file

@ -8,7 +8,7 @@ import { Program } from "./program"
import { TypeChecker } from "./checker" import { TypeChecker } from "./checker"
import { Evaluator } from "./evaluator" import { Evaluator } from "./evaluator"
import { emit } from "./emitter" import { emit } from "./emitter"
import { Syntax, BoltSourceFile } from "./ast" import { Syntax, BoltSourceFile, SourceFile } from "./ast"
import { upsearchSync, FastStringMap, getFileStem, getLanguage } from "./util" import { upsearchSync, FastStringMap, getFileStem, getLanguage } from "./util"
import { Package } from "./package" import { Package } from "./package"
import { verbose, memoize } from "./util" import { verbose, memoize } from "./util"
@ -115,18 +115,13 @@ export class Frontend {
} }
for (const sourceFile of program.getAllSourceFiles()) { for (const sourceFile of program.getAllSourceFiles()) {
//const filepath = rootNode.span!.file.fullPath;
//const pkg = this.getPackage(filepath);
//if (pkg !== null) {
//
//}
fs.mkdirp('.bolt-work'); fs.mkdirp('.bolt-work');
fs.writeFileSync(this.mapToTargetFile(sourceFile), emit(sourceFile), 'utf8'); fs.writeFileSync(this.mapToTargetFile(sourceFile), emit(sourceFile), 'utf8');
} }
} }
private mapToTargetFile(node: Syntax) { private mapToTargetFile(node: SourceFile) {
return path.join('.bolt-work', getFileStem(node.span!.file.fullPath) + getDefaultExtension(getLanguage(node))); return path.join('.bolt-work', getFileStem(node.span!.file.fullPath) + getDefaultExtension(getLanguage(node)));
} }

View file

@ -1,9 +0,0 @@
export class Package {
constructor(public rootDir: string) {
}
}

View file

@ -16,7 +16,7 @@ import {
BoltQualName, BoltQualName,
BoltPattern, BoltPattern,
createBoltBindPattern, createBoltBindPattern,
BoltImportDeclaration, BoltImportDirective,
BoltTypeExpression, BoltTypeExpression,
createBoltReferenceTypeExpression, createBoltReferenceTypeExpression,
createBoltConstantExpression, createBoltConstantExpression,
@ -25,8 +25,8 @@ import {
BoltBindPattern, BoltBindPattern,
createBoltRecordDeclaration, createBoltRecordDeclaration,
createBoltRecordField, createBoltRecordField,
createBoltImportDeclaration, createBoltImportDirective,
BoltDeclarationModifiers, BoltModifiers,
BoltStringLiteral, BoltStringLiteral,
BoltImportSymbol, BoltImportSymbol,
BoltExpressionStatement, BoltExpressionStatement,
@ -77,6 +77,7 @@ import {
BoltMacroCall, BoltMacroCall,
createBoltMacroCall, createBoltMacroCall,
createBoltMemberExpression, createBoltMemberExpression,
BoltSourceFileModifiers,
} from "./ast" } from "./ast"
import { parseForeignLanguage } from "./foreign" import { parseForeignLanguage } from "./foreign"
@ -88,6 +89,7 @@ import {
ParseError, ParseError,
setOrigNodeRange, setOrigNodeRange,
createTokenStream, createTokenStream,
Package,
} from "./common" } from "./common"
import { Stream, uniq } from "./util" import { Stream, uniq } from "./util"
@ -140,6 +142,7 @@ const KIND_DECLARATION_T0 = uniq([
const KIND_SOURCEELEMENT_T0 = uniq([ const KIND_SOURCEELEMENT_T0 = uniq([
SyntaxKind.BoltModKeyword, SyntaxKind.BoltModKeyword,
SyntaxKind.BoltImportKeyword,
...KIND_EXPRESSION_T0, ...KIND_EXPRESSION_T0,
...KIND_STATEMENT_T0, ...KIND_STATEMENT_T0,
...KIND_DECLARATION_T0, ...KIND_DECLARATION_T0,
@ -321,7 +324,13 @@ export class Parser {
} }
} }
public parseImportDeclaration(tokens: BoltTokenStream): BoltImportDeclaration { public parseImportDirective(tokens: BoltTokenStream): BoltImportDirective {
let modifiers = 0;
if (tokens.peek().kind === SyntaxKind.BoltPubKeyword) {
tokens.get();
modifiers |= BoltModifiers.IsPublic;
}
const t0 = tokens.get(); const t0 = tokens.get();
assertToken(t0, SyntaxKind.BoltImportKeyword); assertToken(t0, SyntaxKind.BoltImportKeyword);
@ -331,9 +340,12 @@ export class Parser {
const filename = (t1 as BoltStringLiteral).value; const filename = (t1 as BoltStringLiteral).value;
const symbols: BoltImportSymbol[] = []; const symbols: BoltImportSymbol[] = [];
const t2 = tokens.get();
if (t2.kind === SyntaxKind.BoltParenthesized) {
// TODO implement grammar and parsing logic for symbols // TODO implement grammar and parsing logic for symbols
}
const node = createBoltImportDeclaration(filename, symbols); const node = createBoltImportDirective(modifiers, filename, symbols);
setOrigNodeRange(node, t0, t1); setOrigNodeRange(node, t0, t1);
return node; return node;
} }
@ -701,7 +713,7 @@ export class Parser {
const t1 = tokens.peek(); const t1 = tokens.peek();
if (t1.kind === SyntaxKind.BoltMutKeyword) { if (t1.kind === SyntaxKind.BoltMutKeyword) {
tokens.get(); tokens.get();
modifiers |= BoltDeclarationModifiers.Mutable; modifiers |= BoltModifiers.Mutable;
} }
const bindings = this.parsePattern(tokens) const bindings = this.parsePattern(tokens)
@ -821,7 +833,7 @@ export class Parser {
let t0 = tokens.get(); let t0 = tokens.get();
const firstToken = t0; const firstToken = t0;
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
t0 = tokens.get(); t0 = tokens.get();
} }
@ -915,7 +927,7 @@ export class Parser {
const firstToken = t0; const firstToken = t0;
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
tokens.get(); tokens.get();
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
t0 = tokens.peek(); t0 = tokens.peek();
} }
@ -946,7 +958,7 @@ export class Parser {
const firstToken = t0; const firstToken = t0;
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
t0 = tokens.get(); t0 = tokens.get();
} }
@ -985,13 +997,12 @@ export class Parser {
if (k0.kind === SyntaxKind.BoltPubKeyword) { if (k0.kind === SyntaxKind.BoltPubKeyword) {
tokens.get(); tokens.get();
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
k0 = tokens.peek(); k0 = tokens.peek();
} }
if (k0.kind === SyntaxKind.BoltForeignKeyword) { if (k0.kind === SyntaxKind.BoltForeignKeyword) {
tokens.get(); tokens.get();
modifiers |= BoltDeclarationModifiers.IsForeign;
const l1 = tokens.get(); const l1 = tokens.get();
if (l1.kind !== SyntaxKind.BoltStringLiteral) { if (l1.kind !== SyntaxKind.BoltStringLiteral) {
throw new ParseError(l1, [SyntaxKind.BoltStringLiteral]) throw new ParseError(l1, [SyntaxKind.BoltStringLiteral])
@ -1150,7 +1161,7 @@ export class Parser {
let t0 = tokens.get(); let t0 = tokens.get();
const firstToken = t0; const firstToken = t0;
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
t0 = tokens.get(); t0 = tokens.get();
} }
assertToken(t0, SyntaxKind.BoltTraitKeyword); assertToken(t0, SyntaxKind.BoltTraitKeyword);
@ -1181,7 +1192,7 @@ export class Parser {
let t0 = tokens.get(); let t0 = tokens.get();
const firstToken = t0; const firstToken = t0;
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
modifiers |= BoltDeclarationModifiers.Public; modifiers |= BoltModifiers.IsPublic;
t0 = tokens.get(); t0 = tokens.get();
} }
assertToken(t0, SyntaxKind.BoltImplKeyword); assertToken(t0, SyntaxKind.BoltImplKeyword);
@ -1244,12 +1255,12 @@ export class Parser {
} }
private getFirstTokenAfterModifiers(tokens: BoltTokenStream): BoltToken { private getFirstTokenAfterModifiers(tokens: BoltTokenStream): BoltToken {
let mustBeDecl = false; let mustBeDeclOrImport = false;
let mustBeFunctionOrVariable = false; let mustBeFunctionOrVariable = false;
let i = 1; let i = 1;
let t0 = tokens.peek(i); let t0 = tokens.peek(i);
if (t0.kind === SyntaxKind.BoltPubKeyword) { if (t0.kind === SyntaxKind.BoltPubKeyword) {
mustBeDecl = true; mustBeDeclOrImport = true;
t0 = tokens.peek(++i); t0 = tokens.peek(++i);
} }
if (t0.kind === SyntaxKind.BoltForeignKeyword) { if (t0.kind === SyntaxKind.BoltForeignKeyword) {
@ -1262,7 +1273,7 @@ export class Parser {
&& t0.kind !== SyntaxKind.BoltFnKeyword) { && t0.kind !== SyntaxKind.BoltFnKeyword) {
throw new ParseError(t0, [SyntaxKind.BoltStructKeyword, SyntaxKind.BoltFnKeyword]); throw new ParseError(t0, [SyntaxKind.BoltStructKeyword, SyntaxKind.BoltFnKeyword]);
} }
if (mustBeDecl && KIND_DECLARATION_T0.indexOf(t0.kind) === -1) { if (mustBeDeclOrImport && KIND_DECLARATION_T0.indexOf(t0.kind) === -1 && t0.kind !== SyntaxKind.BoltImportKeyword) {
throw new ParseError(t0, KIND_DECLARATION_KEYWORD); throw new ParseError(t0, KIND_DECLARATION_KEYWORD);
} }
return t0; return t0;
@ -1278,9 +1289,12 @@ export class Parser {
return this.parseMacroCall(tokens); return this.parseMacroCall(tokens);
} }
const t0 = this.getFirstTokenAfterModifiers(tokens); const t0 = this.getFirstTokenAfterModifiers(tokens);
if (KIND_STATEMENT_T0.indexOf(t0.kind) !== -1) { if (t0.kind === SyntaxKind.BoltImportKeyword) {
return this.parseImportDirective(tokens);
} else if (KIND_STATEMENT_T0.indexOf(t0.kind) !== -1) {
return this.parseStatement(tokens); return this.parseStatement(tokens);
} }
// FIXME This should only parse when the tokens are valid and otherwise display a friendly error message.
return this.parseDeclaration(tokens); return this.parseDeclaration(tokens);
} }
@ -1372,12 +1386,13 @@ export class Parser {
// return lhs // return lhs
//} //}
public parseSourceFile(tokens: BoltTokenStream): BoltSourceFile { public parseSourceFile(tokens: BoltTokenStream, pkg: Package): BoltSourceFile {
const elements = this.parseSourceElements(tokens); const elements = this.parseSourceElements(tokens);
const t1 = tokens.peek(); const t1 = tokens.peek();
assertToken(t1, SyntaxKind.EndOfFile); assertToken(t1, SyntaxKind.EndOfFile);
return createBoltSourceFile( return createBoltSourceFile(
elements, elements,
pkg,
new TextSpan(t1.span!.file, new TextPos(0,1,1), t1.span!.end.clone()) new TextSpan(t1.span!.file, new TextPos(0,1,1), t1.span!.end.clone())
); );
} }
@ -1518,12 +1533,12 @@ import * as fs from "fs"
import {JSScanner} from "./foreign/js/scanner"; import {JSScanner} from "./foreign/js/scanner";
import {emit} from "./emitter"; import {emit} from "./emitter";
export function parseSourceFile(filepath: string): BoltSourceFile { export function parseSourceFile(filepath: string, pkg: Package): BoltSourceFile {
const file = new TextFile(filepath); const file = new TextFile(filepath);
const contents = fs.readFileSync(file.origPath, 'utf8'); const contents = fs.readFileSync(file.origPath, 'utf8');
const scanner = new Scanner(file, contents) const scanner = new Scanner(file, contents)
const parser = new Parser(); const parser = new Parser();
const sourceFile = parser.parseSourceFile(scanner); const sourceFile = parser.parseSourceFile(scanner, pkg);
setParents(sourceFile); setParents(sourceFile);
return sourceFile; return sourceFile;
} }

View file

@ -1,4 +1,5 @@
import { Package } from "./common"
import { SourceFile } from "./ast" import { SourceFile } from "./ast"
import { FastStringMap } from "./util"; import { FastStringMap } from "./util";
@ -7,12 +8,14 @@ export class Program {
private transformed = new FastStringMap<string, SourceFile>(); private transformed = new FastStringMap<string, SourceFile>();
constructor( constructor(
sourceFiles: SourceFile[] pkgs: Package[]
) { ) {
for (const sourceFile of sourceFiles) { for (const pkg of pkgs) {
for (const sourceFile of pkg.sourceFiles) {
this.transformed.set(sourceFile.span!.file.fullPath, sourceFile); this.transformed.set(sourceFile.span!.file.fullPath, sourceFile);
} }
} }
}
public getAllSourceFiles() { public getAllSourceFiles() {
return this.transformed.values(); return this.transformed.values();

View file

@ -9,6 +9,8 @@ import {
import { import {
BoltToken, BoltToken,
createBoltImportKeyword,
createBoltExportKeyword,
createBoltRArrowAlt, createBoltRArrowAlt,
createEndOfFile, createEndOfFile,
createBoltIdentifier, createBoltIdentifier,
@ -308,6 +310,8 @@ export class Scanner {
case 'trait': return createBoltTraitKeyword(span); case 'trait': return createBoltTraitKeyword(span);
case 'impl': return createBoltImplKeyword(span); case 'impl': return createBoltImplKeyword(span);
case 'type': return createBoltTypeKeyword(span); case 'type': return createBoltTypeKeyword(span);
//case 'export': return createBoltExportKeyword(span);
case 'import': return createBoltImportKeyword(span);
case 'foreign': return createBoltForeignKeyword(span); case 'foreign': return createBoltForeignKeyword(span);
case 'let': return createBoltLetKeyword(span); case 'let': return createBoltLetKeyword(span);
case 'mut': return createBoltMutKeyword(span); case 'mut': return createBoltMutKeyword(span);

View file

@ -58,6 +58,8 @@ function isSyntax(value) {
&& value.__NODE_TYPE !== undefined; && value.__NODE_TYPE !== undefined;
} }
exported.isSyntax = isSyntax;
let nextNodeId = 1; let nextNodeId = 1;
function createNode(nodeType) { function createNode(nodeType) {

View file

@ -4,6 +4,8 @@ import * as path from "path"
const PACKAGE_ROOT = path.resolve(__dirname, '..', '..'); const PACKAGE_ROOT = path.resolve(__dirname, '..', '..');
const CUSTOM_TYPES = ['Package'];
import { Syntax, Declaration, NodeDeclaration, TypeDeclaration, EnumDeclaration, TypeNode, NodeField } from "./ast" import { Syntax, Declaration, NodeDeclaration, TypeDeclaration, EnumDeclaration, TypeNode, NodeField } from "./ast"
import { MapLike } from "../util" import { MapLike } from "../util"
import { FileWriter } from "./util" import { FileWriter } from "./util"
@ -96,6 +98,13 @@ export function generateAST(decls: Declaration[]) {
// Write corresponding TypeScript declarations // Write corresponding TypeScript declarations
// FIXME These imports are specific to our project and should somehow
// form part of the user specification.
dtsFile.write('\nimport { Package } from "./common"\n\n');
dtsFile.write('export function isSyntax(value: any): value is Syntax;\n\n');
dtsFile.write(`\nexport const enum SyntaxKind {\n`); dtsFile.write(`\nexport const enum SyntaxKind {\n`);
for (const decl of leafNodes) { for (const decl of leafNodes) {
dtsFile.write(` ${decl.name} = ${decl.index},\n`); dtsFile.write(` ${decl.name} = ${decl.index},\n`);
@ -111,6 +120,7 @@ export function setParents(node: Syntax): void;
export type SyntaxRange = [Syntax, Syntax]; export type SyntaxRange = [Syntax, Syntax];
interface SyntaxBase<K extends SyntaxKind> { interface SyntaxBase<K extends SyntaxKind> {
id: number;
kind: K; kind: K;
parentNode: ParentTypesOf<K> | null; parentNode: ParentTypesOf<K> | null;
span: TextSpan | null; span: TextSpan | null;
@ -227,6 +237,8 @@ export type ResolveSyntaxKind<K extends SyntaxKind> = Extract<Syntax, { kind: K
return `${emitTypeScriptType(typeNode.typeArgs[0])} | null`; return `${emitTypeScriptType(typeNode.typeArgs[0])} | null`;
} else if (typeNode.name === 'Vec') { } else if (typeNode.name === 'Vec') {
return `${emitTypeScriptType(typeNode.typeArgs[0])}[]`; return `${emitTypeScriptType(typeNode.typeArgs[0])}[]`;
} else if (CUSTOM_TYPES.indexOf(typeNode.name) !== -1) {
return typeNode.name;
} else if (typeNode.name === 'String') { } else if (typeNode.name === 'String') {
return `string`; return `string`;
} else if (typeNode.name === 'Int') { } else if (typeNode.name === 'Int') {

View file

@ -14,6 +14,14 @@ export interface JsonArray extends Array<Json> { };
export interface JsonObject { [key: string]: Json } export interface JsonObject { [key: string]: Json }
export type Json = null | string | boolean | number | JsonArray | JsonObject; export type Json = null | string | boolean | number | JsonArray | JsonObject;
export function isString(value: any): boolean {
return typeof value === 'string';
}
export function hasOwnProperty<T extends object, K extends PropertyKey>(obj: T, key: K): boolean {
return Object.prototype.hasOwnProperty.call(obj, key);
}
export function uniq<T>(elements: T[]): T[] { export function uniq<T>(elements: T[]): T[] {
const out: T[] = []; const out: T[] = [];
const visited = new Set<T>(); const visited = new Set<T>();