Fix support for single-character tokens
This commit is contained in:
parent
d06c8f577d
commit
96aa6b6991
3 changed files with 71 additions and 11 deletions
42
src/ast.ts
42
src/ast.ts
|
@ -14,6 +14,8 @@ export enum SyntaxKind {
|
||||||
Operator,
|
Operator,
|
||||||
Punctuated,
|
Punctuated,
|
||||||
Semi,
|
Semi,
|
||||||
|
Comma,
|
||||||
|
Colon,
|
||||||
|
|
||||||
// Special nodes
|
// Special nodes
|
||||||
|
|
||||||
|
@ -242,6 +244,46 @@ export class Semi extends SyntaxBase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Colon extends SyntaxBase {
|
||||||
|
|
||||||
|
kind: SyntaxKind.Colon = SyntaxKind.Colon;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public span: TextSpan,
|
||||||
|
public parentNode: Syntax | null = null
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return {
|
||||||
|
kind: 'Colon',
|
||||||
|
span: this.span.toJSON(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Comma extends SyntaxBase {
|
||||||
|
|
||||||
|
kind: SyntaxKind.Comma = SyntaxKind.Comma;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public span: TextSpan,
|
||||||
|
public parentNode: Syntax | null = null
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return {
|
||||||
|
kind: 'Comma',
|
||||||
|
span: this.span.toJSON(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export type Token
|
export type Token
|
||||||
= Semi
|
= Semi
|
||||||
| Identifier
|
| Identifier
|
||||||
|
|
|
@ -13,7 +13,10 @@ import {
|
||||||
Decl,
|
Decl,
|
||||||
Punctuated,
|
Punctuated,
|
||||||
Sentence,
|
Sentence,
|
||||||
SourceFile
|
SourceFile,
|
||||||
|
Semi,
|
||||||
|
Comma,
|
||||||
|
Colon
|
||||||
} from "./ast"
|
} from "./ast"
|
||||||
|
|
||||||
function escapeChar(ch: string) {
|
function escapeChar(ch: string) {
|
||||||
|
@ -189,6 +192,19 @@ export class Scanner {
|
||||||
|
|
||||||
const startPos = this.currPos.clone()
|
const startPos = this.currPos.clone()
|
||||||
|
|
||||||
|
switch (c0) {
|
||||||
|
case ';':
|
||||||
|
this.getChar();
|
||||||
|
return new Semi(new TextSpan(this.file, startPos, this.currPos.clone()));
|
||||||
|
case ',':
|
||||||
|
this.getChar();
|
||||||
|
return new Comma(new TextSpan(this.file, startPos, this.currPos.clone()));
|
||||||
|
case ':':
|
||||||
|
this.getChar();
|
||||||
|
return new Colon(new TextSpan(this.file, startPos, this.currPos.clone()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (isOpenPunct(c0)) {
|
if (isOpenPunct(c0)) {
|
||||||
|
|
||||||
this.getChar();
|
this.getChar();
|
||||||
|
@ -257,14 +273,18 @@ export class Scanner {
|
||||||
const elements: Decl[] = []
|
const elements: Decl[] = []
|
||||||
const startPos = this.currPos.clone()
|
const startPos = this.currPos.clone()
|
||||||
|
|
||||||
while (true) {
|
outer: while (true) {
|
||||||
|
|
||||||
const tokens: Token[] = [];
|
const tokens: Token[] = [];
|
||||||
|
|
||||||
while (true) {
|
inner: while (true) {
|
||||||
const token = this.scanToken();
|
const token = this.scanToken();
|
||||||
if (token === null) {
|
if (token === null) {
|
||||||
break;
|
if (tokens.length === 0) {
|
||||||
|
break outer;
|
||||||
|
} else {
|
||||||
|
break inner;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (token.kind === SyntaxKind.Semi) {
|
if (token.kind === SyntaxKind.Semi) {
|
||||||
break;
|
break;
|
||||||
|
@ -275,12 +295,10 @@ export class Scanner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tokens.length === 0) {
|
if (tokens.length > 0) {
|
||||||
break;
|
elements.push(new Sentence(tokens, new TextSpan(this.file, tokens[0].span.start.clone(), tokens[tokens.length-1].span.end.clone())))
|
||||||
}
|
}
|
||||||
|
|
||||||
elements.push(new Sentence(tokens, new TextSpan(this.file, tokens[0].span.start.clone(), tokens[tokens.length-1].span.end.clone())))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const endPos = this.currPos.clone();
|
const endPos = this.currPos.clone();
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{ foo baz bar }
|
{ foo baz bar };
|
||||||
( foo baz bar )
|
( foo baz bar );
|
||||||
[ foo baz bar ]
|
[ foo baz bar ];
|
||||||
|
|
Loading…
Reference in a new issue