drift-parser 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ast.json +72 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/src/ast/ast.d.ts +0 -0
- package/dist/src/ast/exports.d.ts +1 -0
- package/dist/src/ast/expr.d.ts +0 -0
- package/dist/src/ast/stmt.d.ts +0 -0
- package/dist/src/ast/type.d.ts +31 -0
- package/dist/src/lexer/exports.d.ts +2 -0
- package/dist/src/lexer/tokenizer.d.ts +36 -0
- package/dist/src/lexer/tokens.d.ts +174 -0
- package/dist/src/parser/exports.d.ts +1 -0
- package/dist/src/parser/expr.d.ts +5 -0
- package/dist/src/parser/lookup.d.ts +28 -0
- package/dist/src/parser/parser.d.ts +23 -0
- package/dist/src/parser/stmt.d.ts +3 -0
- package/dist/src/parser/type.d.ts +0 -0
- package/dist/src/utils/combineLocation.d.ts +2 -0
- package/dist/src/utils/genexpr.d.ts +16 -0
- package/dist/src/utils/mapAll.d.ts +0 -0
- package/dist/src/utils/registerParse.d.ts +7 -0
- package/index.d.ts +1 -0
- package/index.ts +2 -0
- package/package.json +22 -0
- package/scripts/build.js +50 -0
- package/src/ast/ast.ts +0 -0
- package/src/ast/exports.ts +3 -0
- package/src/ast/expr.ts +122 -0
- package/src/ast/stmt.ts +126 -0
- package/src/ast/type.ts +46 -0
- package/src/lexer/exports.ts +2 -0
- package/src/lexer/tokenizer.ts +395 -0
- package/src/lexer/tokens.ts +241 -0
- package/src/parser/exports.ts +1 -0
- package/src/parser/expr.ts +82 -0
- package/src/parser/lookup.ts +69 -0
- package/src/parser/parser.ts +166 -0
- package/src/parser/stmt.ts +151 -0
- package/src/parser/type.ts +89 -0
- package/src/utils/combineLocation.ts +7 -0
- package/src/utils/mapAll.ts +43 -0
- package/src/utils/registerParse.ts +117 -0
- package/tests/astTest.js +44 -0
- package/tests/printTest.mjs +7 -0
- package/tests/tokenize.js +92 -0
- package/tests/typenames.js +15 -0
- package/tsconfig.json +15 -0
package/ast.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "BlockStatement",
|
|
3
|
+
"loc": {
|
|
4
|
+
"start": {
|
|
5
|
+
"line": 1,
|
|
6
|
+
"col": 0
|
|
7
|
+
},
|
|
8
|
+
"end": {
|
|
9
|
+
"line": 1,
|
|
10
|
+
"col": 2
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"range": [
|
|
14
|
+
0,
|
|
15
|
+
2
|
|
16
|
+
],
|
|
17
|
+
"body": [
|
|
18
|
+
{
|
|
19
|
+
"type": "ExpressionStatement",
|
|
20
|
+
"loc": {
|
|
21
|
+
"start": {
|
|
22
|
+
"line": 1,
|
|
23
|
+
"col": 0
|
|
24
|
+
},
|
|
25
|
+
"end": {
|
|
26
|
+
"line": 1,
|
|
27
|
+
"col": 2
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"range": [
|
|
31
|
+
0,
|
|
32
|
+
2
|
|
33
|
+
],
|
|
34
|
+
"expression": {
|
|
35
|
+
"type": "NumberExpression",
|
|
36
|
+
"loc": {
|
|
37
|
+
"start": {
|
|
38
|
+
"line": 1,
|
|
39
|
+
"col": 0
|
|
40
|
+
},
|
|
41
|
+
"end": {
|
|
42
|
+
"line": 1,
|
|
43
|
+
"col": 1
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"range": [
|
|
47
|
+
0,
|
|
48
|
+
1
|
|
49
|
+
],
|
|
50
|
+
"value": "5"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"type": "TerminatorStatement",
|
|
55
|
+
"loc": {
|
|
56
|
+
"start": {
|
|
57
|
+
"line": 1,
|
|
58
|
+
"col": 1
|
|
59
|
+
},
|
|
60
|
+
"end": {
|
|
61
|
+
"line": 1,
|
|
62
|
+
"col": 2
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"range": [
|
|
66
|
+
1,
|
|
67
|
+
2
|
|
68
|
+
],
|
|
69
|
+
"kind": "semicolon"
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";var L=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var u=(n,r)=>L(n,"name",{value:r,configurable:!0});var W=(n,r)=>{for(var e in r)L(n,e,{get:r[e],enumerable:!0})},V=(n,r,e,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Y(r))!z.call(n,o)&&o!==e&&L(n,o,{get:()=>r[o],enumerable:!(i=X(r,o))||i.enumerable});return n};var q=n=>V(L({},"__esModule",{value:!0}),n);var et={};W(et,{Lexer:()=>M,Parser:()=>_});module.exports=q(et);var M={};W(M,{CreateLexer:()=>G,Lexer:()=>N,Print:()=>T,ReservedTokens:()=>U,Token:()=>g,TokenType:()=>m,Tokenize:()=>y,TypeName:()=>I});var m=(t=>(t[t.EOF=0]="EOF",t[t.WHITESPACE=1]="WHITESPACE",t[t.NEWLINE=2]="NEWLINE",t[t.LIFETIME_EXTEND=3]="LIFETIME_EXTEND",t[t.NULL=4]="NULL",t[t.TRUE=5]="TRUE",t[t.FALSE=6]="FALSE",t[t.NUMBER=7]="NUMBER",t[t.STRING=8]="STRING",t[t.IDENTIFIER=9]="IDENTIFIER",t[t.ARRAYTYPE_IDENTIFIER=10]="ARRAYTYPE_IDENTIFIER",t[t.LEFT_BRACKET=11]="LEFT_BRACKET",t[t.RIGHT_BRACKET=12]="RIGHT_BRACKET",t[t.LEFT_BRACE=13]="LEFT_BRACE",t[t.RIGHT_BRACE=14]="RIGHT_BRACE",t[t.LEFT_PAREN=15]="LEFT_PAREN",t[t.RIGHT_PAREN=16]="RIGHT_PAREN",t[t.ASSINGMENT_EQUALS=17]="ASSINGMENT_EQUALS",t[t.EQUALS=18]="EQUALS",t[t.BYTE_EQUALS=19]="BYTE_EQUALS",t[t.NOT_EQUALS=20]="NOT_EQUALS",t[t.LESS=21]="LESS",t[t.LESS_EQUALS=22]="LESS_EQUALS",t[t.GREATER=23]="GREATER",t[t.GREATER_EQUALS=24]="GREATER_EQUALS",t[t.AND=25]="AND",t[t.OR=26]="OR",t[t.NOT=27]="NOT",t[t.DOT=28]="DOT",t[t.RANGE_OPERATOR=29]="RANGE_OPERATOR",t[t.SPREAD_OPERATOR=30]="SPREAD_OPERATOR",t[t.SEMI_COLON=31]="SEMI_COLON",t[t.COLON=32]="COLON",t[t.QUESTION_OPERATOR=33]="QUESTION_OPERATOR",t[t.COMMA=34]="COMMA",t[t.PLUS_PLUS=35]="PLUS_PLUS",t[t.MINUS_MINUS=36]="MINUS_MINUS",t[t.PLUS_EQUALS=37]="PLUS_EQUALS",t[t.MINUS_EQUALS=38]="MINUS_EQUALS",t[t.MUL_EQUALS=39]="MUL_EQUALS",t[t.DIV_EQUALS=40]="DIV_EQUALS",t[t.MOD_EQUALS=41]="MOD_EQUALS",t[t.EXPONENTIATION_EQUALS=42]="EXPONENTIATION_EQUALS",t[t.PLUS=43]="PLUS",t[t.MINUS=44]="MINUS",t[t.DIVIDE=45]="DIVIDE",t[t.MUL=46]="MUL",t[t.MODULO=47]="MODULO",t[t.EXPONENTATION=48]="EXPONENTATION",t[t.BW_NOT=49]="BW_NOT",t[t.BW_AND=50]="BW_AND",t[t.BW_OR=51]="BW_OR",t[t.BW_XOR=52]="BW_XOR",t[t.BW_LEFTSHIFT=53]="BW_LEFTSHIFT",t[t.BW_RIGHTSHIFT=54]="BW_RIGHTSHIFT",t[t.BW_UNSIGNED_RIGHTSHIFT=55]="BW_UNSIGNED_RIGHTSHIFT",t[t.BW_ROL=56]="BW_ROL",t[t.BW_ROR=57]="BW_ROR",t[t.BW_AND_EQUALS=58]="BW_AND_EQUALS",t[t.BW_OR_EQUALS=59]="BW_OR_EQUALS",t[t.BW_XOR_EQUALS=60]="BW_XOR_EQUALS",t[t.BW_LEFTSHIFT_EQUALS=61]="BW_LEFTSHIFT_EQUALS",t[t.BW_RIGHTSHIFT_EQUALS=62]="BW_RIGHTSHIFT_EQUALS",t[t.BW_ROL_EQUALS=63]="BW_ROL_EQUALS",t[t.BW_ROR_EQUALS=64]="BW_ROR_EQUALS",t[t.SINGLE_COMMENT=65]="SINGLE_COMMENT",t[t.MULTI_COMMENT=66]="MULTI_COMMENT",t[t.LET=67]="LET",t[t.CONST=68]="CONST",t[t.PRIMITIVE_TYPE=69]="PRIMITIVE_TYPE",t[t.MYRESP=70]="MYRESP",t[t.TOFREE=71]="TOFREE",t[t.IMPORT=72]="IMPORT",t[t.AS=73]="AS",t[t.TURBOFISH=74]="TURBOFISH",t[t.CLASS=75]="CLASS",t[t.NEW=76]="NEW",t[t.FN=77]="FN",t[t.RETURN=78]="RETURN",t[t.IF=79]="IF",t[t.ELSE=80]="ELSE",t[t.ELSEIF=81]="ELSEIF",t[t.FOR=82]="FOR",t[t.FOR_EACH=83]="FOR_EACH",t[t.WHILE=84]="WHILE",t[t.EXPORT=85]="EXPORT",t[t.TYPENAME=86]="TYPENAME",t[t.STRUCT=87]="STRUCT",t[t.USE=88]="USE",t[t.UNRWAP=89]="UNRWAP",t[t.AT_COMPILE=90]="AT_COMPILE",t))(m||{}),U={let:67,const:68,import:72,as:73,class:75,new:76,fn:77,return:78,if:79,else:80,elseif:81,for:82,foreach:83,while:84,export:85,typename:86,struct:87,use:88,unwrap:89};function I(n){return m[n]}u(I,"TypeName");var b=class b{constructor(r,e,i,o){this.type=r,this.name=I(r),this.value=e,this.loc=i,this.range=o}IsA(...r){for(let e=0;e<r.length;e++)if(this.type==r[e])return!0;return!1}Print(){let{IDENTIFIER:r,NUMBER:e,STRING:i,SINGLE_COMMENT:o,MULTI_COMMENT:c}=m;this.IsA(r,e,i,o,c)?console.log(`${I(this.type)}(${this.value})`):console.log(`${I(this.type)}()`)}toString(){return this.value}};u(b,"Token");var g=b;var P=class P{constructor(r,e){this.source=r,this.patterns=e,this.Tokens=[],this.pos=0,this.line=1,this.col=0}remainingSource(){return this.source.slice(this.pos)}end_of_file(){return this.pos>=this.source.length}push(r){this.Tokens.push(r)}};u(P,"Lexer");var N=P;function D(n){return(r,e)=>{let i=r.remainingSource().match(e);if(i&&i.index===0){let o=i[0],c={line:r.line,col:r.col},l=r.line,s=r.col;for(let f of o)f===`
|
|
2
|
+
`?(l++,s=0):s++;let E={start:c,end:{line:l,col:s}},h=[r.pos,r.pos+o.length];r.push(new g(n,o,E,h)),r.pos+=o.length,r.line=l,r.col=s}}}u(D,"stringHandler");function Q(n){return(r,e)=>{let i=r.remainingSource().match(e);if(i&&i.index===0){let o=i[0],c={line:r.line,col:r.col},l=r.line,s=r.col;for(let f of o)f===`
|
|
3
|
+
`?(l++,s=0):s++;let E={start:c,end:{line:l,col:s}},h=[r.pos,r.pos+o.length];r.push(new g(n,o,E,h)),r.pos+=o.length,r.line=l,r.col=s}}}u(Q,"numberHandler");function j(n){return(r,e)=>{let i=r.remainingSource().match(e);if(i&&i.index===0){let o=i[0],c={line:r.line,col:r.col},l=r.line,s=r.col;for(let f of o)f===`
|
|
4
|
+
`?(l++,s=0):s++;let E={start:c,end:{line:l,col:s}},h=[r.pos,r.pos+o.length];r.push(new g(n,o,E,h)),r.pos+=o.length,r.line=l,r.col=s}}}u(j,"multiCommentHandler");function Z(n){return(r,e)=>{let o=r.remainingSource().match(e);if(o&&o.index===0){let c=o[0],l={line:r.line,col:r.col},s=r.col+c.length,d={line:r.line,col:s},E={start:l,end:d},h=[r.pos,r.pos+c.length];r.push(new g(n,c,E,h)),r.pos+=c.length,r.col=s}}}u(Z,"singleCommentHandler");function J(n){return(r,e)=>{let i=r.remainingSource().match(e);if(i&&i.index===0){let o=i[0],c={line:r.line,col:r.col},l=r.line,s=r.col;for(let f of o)f===`
|
|
5
|
+
`?(l++,s=0):s++;let E={start:c,end:{line:l,col:s}},h=[r.pos,r.pos+o.length];r.push(new g(n,o,E,h)),r.pos+=o.length,r.line=l,r.col=s}}}u(J,"whitespaceHandler");function a(n,r){return(e,i)=>{let c=e.remainingSource().match(i);if(c&&c.index===0){let l=c[0],s={line:e.line,col:e.col},d=e.line,E=e.col;for(let p of l)p===`
|
|
6
|
+
`?(d++,E=0):E++;let f={start:s,end:{line:d,col:E}},A=[e.pos,e.pos+l.length];e.push(new g(n,r,f,A)),e.pos+=l.length,e.line=d,e.col=E}}}u(a,"defaultHandler");function k(n){return(r,e)=>{let o=r.remainingSource().match(e);if(o&&o.index===0){let c=o[0],l={line:r.line,col:r.col},s=r.line,d=r.col;for(let p of c)p===`
|
|
7
|
+
`?(s++,d=0):d++;let h={start:l,end:{line:s,col:d}},f=[r.pos,r.pos+c.length],A=n[c]??9;r.push(new g(A,c,h,f)),r.pos+=c.length,r.line=s,r.col=d}}}u(k,"symbolHandler");function G(n){let r=[{regex:/^[ \t]+/,handler:J(1)},{regex:/^(?:\r\n|\r|\n)/,handler:a(2,`
|
|
8
|
+
`)},{regex:/^\/\/[^\r\n]*/,handler:Z(65)},{regex:/^\/\*[\s\S]*?\*\//,handler:j(66)},{regex:/^"(?:[^"\\]|\\.)*"/,handler:D(8)},{regex:/^'(?:[^'\\]|\\.)*'/,handler:D(8)},{regex:/^\d+\.\d+/,handler:Q(7)},{regex:/^\d+/,handler:Q(7)},{regex:/^[A-Za-z_][A-Za-z0-9_]*!?$/,handler:k(U)},{regex:/^\[/,handler:a(11,"[")},{regex:/^\]/,handler:a(12,"]")},{regex:/^\{/,handler:a(13,"{")},{regex:/^\}/,handler:a(14,"}")},{regex:/^\(/,handler:a(15,"(")},{regex:/^\)/,handler:a(16,")")},{regex:/^==/,handler:a(18,"==")},{regex:/^!=/,handler:a(20,"!=")},{regex:/^<=/,handler:a(22,"<=")},{regex:/^</,handler:a(21,"<")},{regex:/^>=/,handler:a(24,">=")},{regex:/^>/,handler:a(23,">")},{regex:/^&&/,handler:a(25,"&&")},{regex:/^\|\|/,handler:a(26,"||")},{regex:/^=/,handler:a(17,"=")},{regex:/^!/,handler:a(27,"!")},{regex:/^\.\./,handler:a(29,"..")},{regex:/^\./,handler:a(28,".")},{regex:/^;/,handler:a(31,";")},{regex:/^:/,handler:a(32,":")},{regex:/^\?/,handler:a(33,"?")},{regex:/^,/,handler:a(34,",")},{regex:/^\+\+/,handler:a(35,"++")},{regex:/^--/,handler:a(36,"--")},{regex:/^\+=/,handler:a(37,"+=")},{regex:/^-=/,handler:a(38,"-=")},{regex:/^\+/,handler:a(43,"+")},{regex:/^-/,handler:a(44,"-")},{regex:/^\//,handler:a(45,"/")},{regex:/^\*/,handler:a(46,"*")},{regex:/^%/,handler:a(47,"%")}];return new N(n,r)}u(G,"CreateLexer");function y(n){let r=G(n);for(;!r.end_of_file();){let i=!1;for(let o of r.patterns){let c=r.remainingSource().match(o.regex);if(c&&c.index===0){o.handler(r,o.regex),i=!0;break}}if(!i){let o=r.remainingSource().slice(0,20);throw new Error(`Unrecognized token at line ${r.line}, column ${r.col}: '${o}'`)}}let e={start:{line:r.line,col:r.col},end:{line:r.line,col:r.col}};return r.push(new g(0,"EOF",e,[r.pos,r.pos])),r.Tokens}u(y,"Tokenize");function T(n,r){if((r==null?void 0:r.shortenWhitespace)??!1)for(let i=0;i<n.length;i++)n[i].type==1&&(n[i].value=" ");return n.slice(0,-1).map(i=>i.value).join("")}u(T,"Print");function x(n){return{kind:1,body:n.body??[],...n}}u(x,"mkexpr");function S(n){return{kind:0,body:n.body??[],...n}}u(S,"mkstmt");var H={},C={},v={},$={};function tt(n,r,e){H[n]=r,v[n]=e}u(tt,"led");function F(n,r){C[n]=r}u(F,"nud");function rt(){F(7,R),F(9,R),F(8,R),tt(44,5,R)}u(rt,"createTokenLookups");rt();function w(n){let r=n.loc?` at ${JSON.stringify(n.loc)}`:"",e=n.range?` [${n.range[0]}, ${n.range[1]}]`:"";return r+e}u(w,"tokenLocationInfo");function B(n,r=0){let e=n.currentTokenKind(),i=C[e];if(!i){let c=n.currentToken();throw new Error(`nud handler expected for token ${m[e]}${w(c)}`)}let o=i(n);for(;(H[n.currentTokenKind()]??0)>r;){let c=n.currentTokenKind(),l=v[c];if(!l){let s=n.currentToken();throw new Error(`led handler expected for token ${m[c]}${w(s)}`)}o=l(n,o,r)}return o}u(B,"parseExpr");function R(n){switch(n.currentTokenKind()){case 7:{let r=n.advance();return x({value:r.value,type:"NumberExpression",loc:r.loc,range:r.range})}case 8:{let r=n.advance();return x({value:r.value,type:"StringExpression",loc:r.loc,range:r.range})}case 9:{let r=n.advance();return x({value:r.value,type:"SymbolExpression",loc:r.loc,range:r.range})}default:{let r=n.currentToken();throw new Error(`Can't create primary expression from token type ${m[r.type]}${w(r)}`)}}}u(R,"parsePrimaryExpr");function K(n){let r=n.currentTokenKind();if(r===31){let s=n.advance();return[S({type:"TerminatorStatement",loc:s.loc,range:s.range,kind:s.type==0?"EOF":s.value==";"?"semicolon":"newline"})]}let e=$[r];if(e){let s=e(n);return Array.isArray(s)?s:[s]}let i=B(n,0),o=n.parseTerminator(),c={start:i.loc.start,end:o.loc.end},l=[i.range[0],o.range[1]];return[S({type:"ExpressionStatement",expression:i,loc:c,range:l}),S({type:"TerminatorStatement",loc:o.loc,range:o.range,kind:o.type==0?"EOF":o.value==";"?"semicolon":"newline"})]}u(K,"parseStmt");var O=class O{constructor(r){this.pos=0;this.allowNewline=!1;this.tokens=r}static parse(r){let e=new O(r),i=[];for(;e.hasTokens();)i.push(...K(e));let o=r.filter(E=>E.type!==0);if(o.length===0)throw new Error("Cannot parse an empty token stream.");let c=o[0],l=o[o.length-1],s={start:c.loc.start,end:l.loc.end},d=[c.range[0],l.range[1]];return S({type:"BlockStatement",body:i,loc:s,range:d})}mark(){return this.pos}reset(r){if(r<0||r>this.tokens.length)throw new Error(`Invalid reset position: ${r}`);this.pos=r}currentToken(){return this.tokens[this.pos]}nextToken(){return this.tokens[this.pos+1]}previousToken(){return this.tokens[this.pos-1]}nthToken(r){return this.tokens[this.pos+r]}advance(){var e;let r=this.currentToken();do this.pos++;while(((e=this.currentToken())==null?void 0:e.type)===1);return r}hasTokens(){return this.pos<this.tokens.length&&this.currentTokenKind()!==0}currentTokenKind(){return this.currentToken().type}expect(r){return this.expectError(r)}expectOne(...r){return this.expectOneError(void 0,...r)}parseTerminator(){let r=this.allowNewline?[31,0]:[31,2,0],e=this.currentToken();return e.type===0?e:this.expectOne(...r)}expectError(r,e){let o=this.currentToken().type;if(o!==r)throw e||(e=`Expected token: ${m[r]} but got ${m[o]}`),new Error(e);return this.advance()}expectOneError(r,...e){let o=this.currentToken().type;if(!e.includes(o))throw r||(r=`Expected token: ${e.map(c=>m[c]).join(" OR ")} but got ${m[o]}`),new Error(r);return this.advance()}};u(O,"Parser");var _=O;0&&(module.exports={Lexer,Parser});
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './type';
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { LocationInterface } from "../lexer/tokens";
|
|
2
|
+
export declare enum NodeKind {
|
|
3
|
+
Statement = 0,
|
|
4
|
+
Expression = 1
|
|
5
|
+
}
|
|
6
|
+
export interface Node {
|
|
7
|
+
kind: NodeKind;
|
|
8
|
+
type: string;
|
|
9
|
+
loc: LocationInterface;
|
|
10
|
+
range: [number, number];
|
|
11
|
+
body: Node[];
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}
|
|
14
|
+
export interface Stmt extends Node {
|
|
15
|
+
kind: NodeKind.Statement;
|
|
16
|
+
}
|
|
17
|
+
export interface Expr extends Node {
|
|
18
|
+
kind: NodeKind.Expression;
|
|
19
|
+
}
|
|
20
|
+
type NodeFieldsRequired = {
|
|
21
|
+
type: string;
|
|
22
|
+
loc: LocationInterface;
|
|
23
|
+
range: [number, number];
|
|
24
|
+
} & Record<string, any>;
|
|
25
|
+
export declare function mkexpr(fields: NodeFieldsRequired & {
|
|
26
|
+
body?: Node[];
|
|
27
|
+
}): Expr;
|
|
28
|
+
export declare function mkstmt(fields: NodeFieldsRequired & {
|
|
29
|
+
body?: Node[];
|
|
30
|
+
}): Stmt;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Token } from "./tokens";
|
|
2
|
+
type RegexHandler = (lex: Lexer, regex: RegExp) => void;
|
|
3
|
+
interface RegexPattern {
|
|
4
|
+
regex: RegExp;
|
|
5
|
+
handler: RegexHandler;
|
|
6
|
+
}
|
|
7
|
+
interface LexerInterface {
|
|
8
|
+
patterns: RegexPattern[];
|
|
9
|
+
Tokens: Token[];
|
|
10
|
+
source: string;
|
|
11
|
+
pos: number;
|
|
12
|
+
line: number;
|
|
13
|
+
col: number;
|
|
14
|
+
remainingSource(): string;
|
|
15
|
+
end_of_file(): boolean;
|
|
16
|
+
push(token: Token): void;
|
|
17
|
+
}
|
|
18
|
+
declare class Lexer implements LexerInterface {
|
|
19
|
+
patterns: RegexPattern[];
|
|
20
|
+
Tokens: Token[];
|
|
21
|
+
source: string;
|
|
22
|
+
pos: number;
|
|
23
|
+
line: number;
|
|
24
|
+
col: number;
|
|
25
|
+
constructor(source: string, patterns: RegexPattern[]);
|
|
26
|
+
remainingSource(): string;
|
|
27
|
+
end_of_file(): boolean;
|
|
28
|
+
push(token: Token): void;
|
|
29
|
+
}
|
|
30
|
+
declare function CreateLexer(source: string): Lexer;
|
|
31
|
+
declare function Tokenize(source: string): Token[];
|
|
32
|
+
interface PrintOptions {
|
|
33
|
+
shortenWhitespace: boolean;
|
|
34
|
+
}
|
|
35
|
+
declare function Print(tokens: Token[], options: PrintOptions | undefined): string;
|
|
36
|
+
export { Lexer, CreateLexer, Tokenize, Print };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
export declare enum TokenType {
|
|
2
|
+
EOF = 0,
|
|
3
|
+
/** SPACES ONLY */
|
|
4
|
+
WHITESPACE = 1,
|
|
5
|
+
/** \n character */
|
|
6
|
+
NEWLINE = 2,
|
|
7
|
+
LIFETIME_EXTEND = 3,// x!
|
|
8
|
+
NULL = 4,
|
|
9
|
+
TRUE = 5,
|
|
10
|
+
FALSE = 6,
|
|
11
|
+
NUMBER = 7,
|
|
12
|
+
STRING = 8,
|
|
13
|
+
IDENTIFIER = 9,
|
|
14
|
+
ARRAYTYPE_IDENTIFIER = 10,
|
|
15
|
+
LEFT_BRACKET = 11,
|
|
16
|
+
RIGHT_BRACKET = 12,
|
|
17
|
+
LEFT_BRACE = 13,
|
|
18
|
+
RIGHT_BRACE = 14,
|
|
19
|
+
LEFT_PAREN = 15,
|
|
20
|
+
RIGHT_PAREN = 16,
|
|
21
|
+
ASSINGMENT_EQUALS = 17,
|
|
22
|
+
/** This is the equality check operator == */
|
|
23
|
+
EQUALS = 18,
|
|
24
|
+
/**
|
|
25
|
+
* b==
|
|
26
|
+
* performs a byte by byte comparison for things like strings like "a" b== "a"
|
|
27
|
+
*/
|
|
28
|
+
BYTE_EQUALS = 19,
|
|
29
|
+
NOT_EQUALS = 20,
|
|
30
|
+
LESS = 21,
|
|
31
|
+
LESS_EQUALS = 22,
|
|
32
|
+
GREATER = 23,
|
|
33
|
+
GREATER_EQUALS = 24,
|
|
34
|
+
AND = 25,
|
|
35
|
+
OR = 26,
|
|
36
|
+
NOT = 27,
|
|
37
|
+
DOT = 28,
|
|
38
|
+
/** range as in : [0..10] */
|
|
39
|
+
RANGE_OPERATOR = 29,
|
|
40
|
+
SPREAD_OPERATOR = 30,
|
|
41
|
+
SEMI_COLON = 31,
|
|
42
|
+
COLON = 32,// for ternary + a lot more
|
|
43
|
+
QUESTION_OPERATOR = 33,// for ternary, eg condition ? result : elseresult
|
|
44
|
+
COMMA = 34,
|
|
45
|
+
PLUS_PLUS = 35,
|
|
46
|
+
MINUS_MINUS = 36,
|
|
47
|
+
PLUS_EQUALS = 37,
|
|
48
|
+
MINUS_EQUALS = 38,
|
|
49
|
+
MUL_EQUALS = 39,// *=
|
|
50
|
+
DIV_EQUALS = 40,// /=
|
|
51
|
+
MOD_EQUALS = 41,// %=
|
|
52
|
+
EXPONENTIATION_EQUALS = 42,// **=
|
|
53
|
+
PLUS = 43,
|
|
54
|
+
MINUS = 44,
|
|
55
|
+
DIVIDE = 45,
|
|
56
|
+
MUL = 46,
|
|
57
|
+
MODULO = 47,
|
|
58
|
+
EXPONENTATION = 48,
|
|
59
|
+
BW_NOT = 49,
|
|
60
|
+
BW_AND = 50,
|
|
61
|
+
BW_OR = 51,
|
|
62
|
+
BW_XOR = 52,
|
|
63
|
+
BW_LEFTSHIFT = 53,
|
|
64
|
+
BW_RIGHTSHIFT = 54,
|
|
65
|
+
BW_UNSIGNED_RIGHTSHIFT = 55,
|
|
66
|
+
BW_ROL = 56,// r<<
|
|
67
|
+
BW_ROR = 57,// r >> - rotate right and left, macros of other bitwise ops that shift but rotate
|
|
68
|
+
BW_AND_EQUALS = 58,// &=
|
|
69
|
+
BW_OR_EQUALS = 59,// |=
|
|
70
|
+
BW_XOR_EQUALS = 60,// ^=
|
|
71
|
+
BW_LEFTSHIFT_EQUALS = 61,// <<=
|
|
72
|
+
BW_RIGHTSHIFT_EQUALS = 62,// >>=
|
|
73
|
+
BW_ROL_EQUALS = 63,// ridiculous but r<<=
|
|
74
|
+
BW_ROR_EQUALS = 64,// r>>=
|
|
75
|
+
SINGLE_COMMENT = 65,//
|
|
76
|
+
MULTI_COMMENT = 66,// ### ... ###
|
|
77
|
+
LET = 67,// maybe change to set?
|
|
78
|
+
CONST = 68,
|
|
79
|
+
PRIMITIVE_TYPE = 69,//u8/byte, string, array(slice), u16 u32 u64 u128 + signed, float<-->f32, double<-->f64, quad<-->quadruple<-->f128
|
|
80
|
+
MYRESP = 70,//my or myresp, can be a prefix to an expr,
|
|
81
|
+
TOFREE = 71,//tf or tofree
|
|
82
|
+
/**
|
|
83
|
+
* import std = "std";
|
|
84
|
+
* import { add, mul } = "math.dr";
|
|
85
|
+
* potentially import module = c "module.h";
|
|
86
|
+
*
|
|
87
|
+
*/
|
|
88
|
+
IMPORT = 72,
|
|
89
|
+
AS = 73,// import { a as b } -- also single type casts if not in an import like 5::<i32> as const,
|
|
90
|
+
TURBOFISH = 74,// :: - sorry original goals, its just nice to do 5::i32 or 5::my instead of (5) as <i32>, you can also do 5::<i32,const> for multiple
|
|
91
|
+
CLASS = 75,
|
|
92
|
+
NEW = 76,
|
|
93
|
+
FN = 77,// func def
|
|
94
|
+
RETURN = 78,
|
|
95
|
+
IF = 79,
|
|
96
|
+
ELSE = 80,
|
|
97
|
+
ELSEIF = 81,
|
|
98
|
+
FOR = 82,
|
|
99
|
+
FOR_EACH = 83,//idk yet
|
|
100
|
+
WHILE = 84,
|
|
101
|
+
EXPORT = 85,
|
|
102
|
+
/**
|
|
103
|
+
* The operator for an `@` decorator above a function.
|
|
104
|
+
*/
|
|
105
|
+
TYPENAME = 86,// typename "hi" ==
|
|
106
|
+
STRUCT = 87,
|
|
107
|
+
/**
|
|
108
|
+
Sets compile options, eg, USE unsafe_features;
|
|
109
|
+
And parser options like USE c_logicals; makes it || and && instead of or / and and ! instead of not.
|
|
110
|
+
*/
|
|
111
|
+
USE = 88,
|
|
112
|
+
/**
|
|
113
|
+
* Example function `a` demonstrating an unwrapped return from function b.
|
|
114
|
+
*
|
|
115
|
+
* ```rust
|
|
116
|
+
* fn a() {
|
|
117
|
+
* const b = unwrap {
|
|
118
|
+
* let abc = 5; //local abc
|
|
119
|
+
* return "top level return" + abc; //only returns the block
|
|
120
|
+
* };
|
|
121
|
+
* return b;
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
UNRWAP = 89,
|
|
126
|
+
AT_COMPILE = 90
|
|
127
|
+
}
|
|
128
|
+
export declare const ReservedTokens: {
|
|
129
|
+
let: TokenType;
|
|
130
|
+
const: TokenType;
|
|
131
|
+
import: TokenType;
|
|
132
|
+
as: TokenType;
|
|
133
|
+
class: TokenType;
|
|
134
|
+
new: TokenType;
|
|
135
|
+
fn: TokenType;
|
|
136
|
+
return: TokenType;
|
|
137
|
+
if: TokenType;
|
|
138
|
+
else: TokenType;
|
|
139
|
+
elseif: TokenType;
|
|
140
|
+
for: TokenType;
|
|
141
|
+
foreach: TokenType;
|
|
142
|
+
while: TokenType;
|
|
143
|
+
export: TokenType;
|
|
144
|
+
typename: TokenType;
|
|
145
|
+
struct: TokenType;
|
|
146
|
+
use: TokenType;
|
|
147
|
+
unwrap: TokenType;
|
|
148
|
+
};
|
|
149
|
+
export declare function TypeName(type: TokenType): string;
|
|
150
|
+
export interface Position {
|
|
151
|
+
line: number;
|
|
152
|
+
col: number;
|
|
153
|
+
}
|
|
154
|
+
export interface LocationInterface {
|
|
155
|
+
start: Position;
|
|
156
|
+
end: Position;
|
|
157
|
+
}
|
|
158
|
+
export declare class Token {
|
|
159
|
+
type: TokenType;
|
|
160
|
+
name: string;
|
|
161
|
+
value: string;
|
|
162
|
+
loc: LocationInterface;
|
|
163
|
+
range: [number, number];
|
|
164
|
+
constructor(type: TokenType, value: string, location: LocationInterface, range: [number, number]);
|
|
165
|
+
/**
|
|
166
|
+
* Checks if the token's type matches any of the provided token types.
|
|
167
|
+
*
|
|
168
|
+
* @param {...TokenType[]} checkTypes The token types to compare to.
|
|
169
|
+
* @returns {boolean} True if this token's type is in the list, otherwise false.
|
|
170
|
+
*/
|
|
171
|
+
IsA(...checkTypes: TokenType[]): boolean;
|
|
172
|
+
Print(): void;
|
|
173
|
+
toString(): string;
|
|
174
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Parser } from './parser';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TokenType } from "../lexer/tokens";
|
|
2
|
+
import { Expr, Stmt } from "../ast/exports";
|
|
3
|
+
import { Parser } from "./parser";
|
|
4
|
+
export declare enum BP {
|
|
5
|
+
Default = 0,
|
|
6
|
+
Comma = 1,
|
|
7
|
+
Assignment = 2,
|
|
8
|
+
Logical = 3,
|
|
9
|
+
Relational = 4,
|
|
10
|
+
Additive = 5,
|
|
11
|
+
Multiplicative = 6,
|
|
12
|
+
Unary = 7,
|
|
13
|
+
Call = 8,
|
|
14
|
+
Member = 9,
|
|
15
|
+
Primary = 10
|
|
16
|
+
}
|
|
17
|
+
export type StmtHandler = (p: Parser) => Stmt[];
|
|
18
|
+
export type NudHandler = (p: Parser) => Expr;
|
|
19
|
+
export type LedHandler = (p: Parser, left: Expr, bp: BP) => Expr;
|
|
20
|
+
declare const _bp_lu: Partial<Record<TokenType, BP>>;
|
|
21
|
+
declare const _nud_lu: Partial<Record<TokenType, NudHandler>>;
|
|
22
|
+
declare const _led_lu: Partial<Record<TokenType, LedHandler>>;
|
|
23
|
+
declare const _stmt_lu: Partial<Record<TokenType, StmtHandler>>;
|
|
24
|
+
export declare function led(kind: TokenType, bp: BP, ledFn: LedHandler): void;
|
|
25
|
+
export declare function nud(kind: TokenType, nudFn: NudHandler): void;
|
|
26
|
+
export declare function stmt(kind: TokenType, stmtFn: StmtHandler): void;
|
|
27
|
+
export declare function createTokenLookups(): void;
|
|
28
|
+
export { _bp_lu as bp_lu, _nud_lu as nud_lu, _led_lu as led_lu, _stmt_lu as stmt_lu, };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Stmt } from "../ast/exports";
|
|
2
|
+
import { Token, TokenType } from "../lexer/tokens";
|
|
3
|
+
export declare class Parser {
|
|
4
|
+
private tokens;
|
|
5
|
+
private pos;
|
|
6
|
+
allowNewline: boolean;
|
|
7
|
+
constructor(tokens: Token[]);
|
|
8
|
+
static parse(tokens: Token[]): Stmt;
|
|
9
|
+
mark(): number;
|
|
10
|
+
reset(pos: number): void;
|
|
11
|
+
currentToken(): Token;
|
|
12
|
+
nextToken(): Token;
|
|
13
|
+
previousToken(): Token;
|
|
14
|
+
nthToken(n: number): Token;
|
|
15
|
+
advance(): Token;
|
|
16
|
+
hasTokens(): boolean;
|
|
17
|
+
currentTokenKind(): TokenType;
|
|
18
|
+
expect(expectedKind: TokenType): Token;
|
|
19
|
+
expectOne(...expectedKind: TokenType[]): Token;
|
|
20
|
+
parseTerminator(): Token;
|
|
21
|
+
expectError(expectedKind: TokenType, err?: string): Token;
|
|
22
|
+
expectOneError(err?: string, ...expectedKind: TokenType[]): Token;
|
|
23
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type ExpressionArgs = Record<string, any>;
|
|
2
|
+
interface ExpressionConfig<TFields extends ExpressionArgs> {
|
|
3
|
+
className: string;
|
|
4
|
+
fields: (keyof TFields)[];
|
|
5
|
+
constructor: (...args: any[]) => {
|
|
6
|
+
loc: any;
|
|
7
|
+
range: any;
|
|
8
|
+
} & TFields;
|
|
9
|
+
toJSON?: (instance: TFields) => Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
export declare function makeExpressionClass<TFields extends ExpressionArgs>(config: ExpressionConfig<TFields>): {
|
|
12
|
+
new (...args: any[]): {
|
|
13
|
+
toJSON(): any;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Stmt } from "../ast/type";
|
|
2
|
+
import { Token } from "../lexer/tokens";
|
|
3
|
+
import { Parser } from "../parser/parser";
|
|
4
|
+
export declare function stmt_registerParse({ consume, handler }: {
|
|
5
|
+
consume: (string | string[])[];
|
|
6
|
+
handler: (...tokens: Token[]) => Stmt[];
|
|
7
|
+
}): (p: Parser) => Stmt[];
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Lexer from "./src/lexer/exports";
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "drift-parser",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "WIP, the parser for the drift programming language",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "node scripts/build.js",
|
|
8
|
+
"build-s": "node scripts/build.js --sourcemap",
|
|
9
|
+
"watch": "node scripts/build.js --watch",
|
|
10
|
+
"watch-s": "node scripts/build.js --watch --sourcemap",
|
|
11
|
+
"def": "tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
12
|
+
"defbuild": "node scripts/build.js && tsc --emitDeclarationOnly --declaration --outDir dist",
|
|
13
|
+
"pub": "npm version patch && node scripts/build.js && tsc --emitDeclarationOnly --declaration --outDir dist && npm publish"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "winter",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"esbuild": "^0.25.4",
|
|
20
|
+
"typescript": "^5.8.3"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/scripts/build.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const esbuild = require('esbuild');
|
|
2
|
+
|
|
3
|
+
const args = process.argv.slice(2);
|
|
4
|
+
const watch = args.includes('--watch');
|
|
5
|
+
const sourcemap = args.includes('--sourcemap') || args.includes('-s');
|
|
6
|
+
|
|
7
|
+
async function build() {
|
|
8
|
+
const start = Date.now();
|
|
9
|
+
|
|
10
|
+
if (watch) {
|
|
11
|
+
const ctx = await esbuild.context({
|
|
12
|
+
entryPoints: ['index.ts'],
|
|
13
|
+
outfile: 'dist/index.js', // Output single bundled file
|
|
14
|
+
platform: 'node',
|
|
15
|
+
bundle: true, // bundle dependencies
|
|
16
|
+
format: 'cjs', // output CommonJS module
|
|
17
|
+
sourcemap,
|
|
18
|
+
minify: true,
|
|
19
|
+
keepNames: true,
|
|
20
|
+
target: ['node14'], // example target, change if needed
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
await ctx.watch();
|
|
24
|
+
|
|
25
|
+
const duration = ((Date.now() - start) / 1000).toFixed(2);
|
|
26
|
+
console.log(`Build finished (watching${sourcemap ? ', sourcemap enabled' : ''}) in \x1b[92m${duration}s\x1b[0m`);
|
|
27
|
+
|
|
28
|
+
process.stdin.resume();
|
|
29
|
+
} else {
|
|
30
|
+
await esbuild.build({
|
|
31
|
+
entryPoints: ['index.ts'],
|
|
32
|
+
outfile: 'dist/index.js',
|
|
33
|
+
platform: 'node',
|
|
34
|
+
bundle: true,
|
|
35
|
+
format: 'cjs',
|
|
36
|
+
minify: true,
|
|
37
|
+
keepNames: true,
|
|
38
|
+
sourcemap,
|
|
39
|
+
target: ['node14'],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const duration = ((Date.now() - start) / 1000).toFixed(2);
|
|
43
|
+
console.log(`Build finished${sourcemap ? ' with sourcemap' : ''} in \x1b[92m${duration}s\x1b[0m`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
build().catch(err => {
|
|
48
|
+
console.error('Build error:', err);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
});
|
package/src/ast/ast.ts
ADDED
|
File without changes
|