MCPcopy Index your code
hub / github.com/aiscript-dev/aiscript / parseVarDef

Function parseVarDef

src/parser/syntaxes/statements.ts:108–145  ·  view source on GitHub ↗

* ```abnf * VarDef = ("let" / "var") Dest [":" Type] "=" Expr * ```

(s: ITokenStream)

Source from the content-addressed store, hash-verified

106 * ```
107*/
108function parseVarDef(s: ITokenStream): Ast.Definition {
109 const startPos = s.getPos();
110
111 let mut: boolean;
112 switch (s.getTokenKind()) {
113 case TokenKind.LetKeyword: {
114 mut = false;
115 break;
116 }
117 case TokenKind.VarKeyword: {
118 mut = true;
119 break;
120 }
121 default: {
122 throw unexpectedTokenError(s.getTokenKind(), s.getPos());
123 }
124 }
125 s.next();
126
127 const dest = parseDest(s);
128
129 let type: Ast.TypeSource | undefined;
130 if (s.is(TokenKind.Colon)) {
131 s.next();
132 type = parseType(s);
133 }
134
135 s.expect(TokenKind.Eq);
136 s.next();
137
138 if (s.is(TokenKind.NewLine)) {
139 s.next();
140 }
141
142 const expr = parseExpr(s, false);
143
144 return NODE('def', { dest, varType: type, expr, mut, attr: [] }, startPos, s.getPos());
145}
146
147/**
148 * ```abnf

Callers 2

parseStatementFunction · 0.85
parseDefStatementFunction · 0.85

Calls 10

unexpectedTokenErrorFunction · 0.85
parseDestFunction · 0.85
parseTypeFunction · 0.85
parseExprFunction · 0.85
NODEFunction · 0.85
getPosMethod · 0.65
getTokenKindMethod · 0.65
nextMethod · 0.65
isMethod · 0.65
expectMethod · 0.65

Tested by

no test coverage detected