Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Apr 23, 2020
1 parent 9cf46d7 commit 7ce294d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
32 changes: 32 additions & 0 deletions src/message/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { SourceLocation } from './location'

export interface CompilerError extends SyntaxError {
code: CompilerErrorCodes
loc?: SourceLocation
}

export const enum CompilerErrorCodes {
MISSING_END_BRACE,
MISSING_END_PAREN,
// Special value for higher-order compilers to pick up the last code
// to avoid collision of error codes. This should always be kept as the last
// item.
__EXTEND_POINT__
}

export const errorMessages: { [code: number]: string } = {
// TODO:
[CompilerErrorCodes.MISSING_END_BRACE]: 'foo'
}

export function createCompilerError(
code: CompilerErrorCodes,
loc?: SourceLocation,
messages?: { [code: number]: string }
): CompilerError {
const msg = __DEV__ ? (messages || errorMessages)[code] : code
const error = new SyntaxError(String(msg)) as CompilerError
error.code = code
error.loc = loc
return error
}
53 changes: 44 additions & 9 deletions src/message/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {
CompilerErrorCodes,
CompilerError,
createCompilerError
} from './errors'
import { SourceLocation, Position } from './location'
import { createTokenizer, Tokenizer, TokenTypes } from './tokenizer'
import { isUnDef } from '../utils'

export const enum NodeTypes {
Resource, // 0
Expand Down Expand Up @@ -72,11 +78,40 @@ export interface LinkedModitierNode extends Node {
value: Identifier
}

export type ParserOptions = {
onError?: (error: CompilerError) => void
}

export type Parser = Readonly<{
parse: (source: string) => ResourceNode
}>

export function createParser(): Parser {
export function createParser(/* options: ParserOptions = {} */): Parser {
// TODO:
/*
const { onError } = options
const emitError = (
code: CompilerErrorCodes,
loc: Position,
offset?: number
): void => {
if (offset) {
loc.offset += offset
loc.column += offset
}
if (onError) {
onError(
createCompilerError(code, {
start: loc,
end: loc,
source: ''
})
)
}
}
*/

const startNode = (type: NodeTypes, offset: number, loc: Position): Node => {
return {
type,
Expand Down Expand Up @@ -113,11 +148,11 @@ export function createParser(): Parser {
return node
}

const parseList = (tokenizer: Tokenizer, index: number): ListNode => {
const parseList = (tokenizer: Tokenizer, index: string): ListNode => {
const context = tokenizer.context()
const { lastOffset: offset, lastStartLoc: loc } = context // get brace left loc
const node = startNode(NodeTypes.List, offset, loc) as ListNode
node.index = index
node.index = parseInt(index, 10)
tokenizer.nextToken() // skip brach right
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition())
return node
Expand Down Expand Up @@ -202,21 +237,21 @@ export function createParser(): Parser {

switch (token.type) {
case TokenTypes.LinkedKey:
if (!token.value || typeof token.value !== 'string') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
linkedNode.key = parseLinkedKey(tokenizer, token.value)
break
case TokenTypes.Named:
if (!token.value || typeof token.value === 'number') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
linkedNode.key = parseNamed(tokenizer, token.value)
break
case TokenTypes.List:
if (token.value === undefined || typeof token.value === 'string') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
Expand Down Expand Up @@ -257,21 +292,21 @@ export function createParser(): Parser {
const token = tokenizer.nextToken()
switch (token.type) {
case TokenTypes.Text:
if (!token.value || typeof token.value === 'number') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
node.items.push(parseText(tokenizer, token.value))
break
case TokenTypes.List:
if (token.value === undefined || typeof token.value === 'string') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
node.items.push(parseList(tokenizer, token.value))
break
case TokenTypes.Named:
if (!token.value || typeof token.value === 'number') {
if (isUnDef(token.value)) {
// TODO: should be thrown syntax error
throw new Error()
}
Expand Down
1 change: 1 addition & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function apply(
app.directive('t', vT as FunctionDirective) // TODO:

// setup global provider
console.log('provide', GlobalI18nSymbol, composer)
app.provide(GlobalI18nSymbol, composer)
}

Expand Down

0 comments on commit 7ce294d

Please sign in to comment.