Skip to content

Commit

Permalink
refactor(bare-parser): minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jun 28, 2023
1 parent edc3781 commit 1a2e9f0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/parser/bare-lex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CompilerError, type Location } from "../core/compiler-error.js"

const WHITE_SPACE_PATTERN = /\s/
const PUNCTUATION_PATTERN = /[\{\}\[\]\(\)<>=\|:,;\.!?~+-\\/$@#]/
const ID_PATTERN = /([A-Za-z0-9_]+)/
const ID_PATTERN = /\w+/

export class Lex {
readonly content: string
Expand Down
7 changes: 3 additions & 4 deletions src/parser/bare-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { Config } from "../core/config.js"
import {
ALL_CASE_RE,
CONSTANT_CASE_RE,
NUMBER_RE,
toPascalCase,
} from "../utils/formatting.js"
import { Lex } from "./bare-lex.js"
Expand Down Expand Up @@ -408,15 +407,15 @@ function checkSeparator(p: Parser): void {
}

function parseUnsignedNumber(p: Parser): number {
const result = Number.parseInt(p.lex.token(), 10)
if (!NUMBER_RE.test(p.lex.token())) {
const token = p.lex.token()
if (!/^\d+$/.test(token)) {
throw new CompilerError(
"an unsigned integer is expected.",
p.lex.location(),
)
}
p.lex.forth()
return result
return Number.parseInt(token, 10)
}

function expect(p: Parser, token: string): void {
Expand Down
1 change: 0 additions & 1 deletion src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
export const ALL_CASE_RE = /^\w+$/
export const CAMEL_CASE_RE = /^[a-z][A-Za-z\d]*$/
export const CONSTANT_CASE_RE = /^[A-Z][A-Z\d_]*$/
export const NUMBER_RE = /^\d+$/
export const PASCAL_CASE_RE = /^[A-Z][A-Za-z\d]*$/

export function capitalize(s: string): string {
Expand Down

0 comments on commit 1a2e9f0

Please sign in to comment.