chore: organize file structure
This commit is contained in:
parent
1c8f5b612a
commit
16fa44bd57
@ -11,7 +11,7 @@ import {
|
|||||||
Position,
|
Position,
|
||||||
TextNode
|
TextNode
|
||||||
} from '../src/ast'
|
} from '../src/ast'
|
||||||
import { ParserErrorTypes } from '../src/errorTypes'
|
import { ParserErrorTypes } from '../src/parserErrorTypes'
|
||||||
import { parserOptionsMinimal as parserOptions } from '../src/parserOptionsMinimal'
|
import { parserOptionsMinimal as parserOptions } from '../src/parserOptionsMinimal'
|
||||||
|
|
||||||
describe('parser/parse', () => {
|
describe('parser/parse', () => {
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
export function assert(condition: boolean, msg?: string) {
|
|
||||||
if (!condition) {
|
|
||||||
throw new Error(msg || `unexpected parser condition`)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +1,3 @@
|
|||||||
export { parse } from './parser'
|
export { parse } from './parser'
|
||||||
export * from './ast'
|
export * from './ast'
|
||||||
export * from './errorTypes'
|
export * from './parserErrorTypes'
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { assert } from './assert'
|
import { ParserErrorTypes } from './parserErrorTypes'
|
||||||
import { ParserErrorTypes } from './errorTypes'
|
|
||||||
import {
|
import {
|
||||||
Node,
|
Node,
|
||||||
AttributeNode,
|
AttributeNode,
|
||||||
@ -60,93 +59,6 @@ export function parse(content: string, options: ParserOptions): RootNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function last<T>(xs: T[]): T | undefined {
|
|
||||||
return xs[xs.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
function startsWith(source: string, searchString: string): boolean {
|
|
||||||
return source.startsWith(searchString)
|
|
||||||
}
|
|
||||||
|
|
||||||
function advanceBy(context: ParserContext, numberOfCharacters: number): void {
|
|
||||||
__DEV__ && assert(numberOfCharacters <= context.source.length)
|
|
||||||
|
|
||||||
const { column, source } = context
|
|
||||||
const str = source.slice(0, numberOfCharacters)
|
|
||||||
const lines = str.split(/\r?\n/)
|
|
||||||
|
|
||||||
context.source = source.slice(numberOfCharacters)
|
|
||||||
context.offset += numberOfCharacters
|
|
||||||
context.line += lines.length - 1
|
|
||||||
context.column =
|
|
||||||
lines.length === 1
|
|
||||||
? column + numberOfCharacters
|
|
||||||
: Math.max(1, lines.pop()!.length)
|
|
||||||
}
|
|
||||||
|
|
||||||
function advanceSpaces(context: ParserContext): void {
|
|
||||||
const match = /^[\t\r\n\f ]+/.exec(context.source)
|
|
||||||
if (match) {
|
|
||||||
advanceBy(context, match[0].length)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCursor(context: ParserContext): Position {
|
|
||||||
const { column, line, offset } = context
|
|
||||||
return { column, line, offset }
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNewPosition(
|
|
||||||
context: ParserContext,
|
|
||||||
start: Position,
|
|
||||||
numberOfCharacters: number
|
|
||||||
): Position {
|
|
||||||
const { originalSource } = context
|
|
||||||
const str = originalSource.slice(start.offset, numberOfCharacters)
|
|
||||||
const lines = str.split(/\r?\n/)
|
|
||||||
|
|
||||||
const newPosition = {
|
|
||||||
column: start.column,
|
|
||||||
line: start.line,
|
|
||||||
offset: start.offset
|
|
||||||
}
|
|
||||||
|
|
||||||
newPosition.offset += numberOfCharacters
|
|
||||||
newPosition.line += lines.length - 1
|
|
||||||
newPosition.column =
|
|
||||||
lines.length === 1
|
|
||||||
? start.column + numberOfCharacters
|
|
||||||
: Math.max(1, lines.pop()!.length)
|
|
||||||
|
|
||||||
return newPosition
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSelection(
|
|
||||||
context: ParserContext,
|
|
||||||
start: Position,
|
|
||||||
end?: Position
|
|
||||||
): SourceLocation {
|
|
||||||
end = end || getCursor(context)
|
|
||||||
return {
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
source: context.originalSource.slice(start.offset, end.offset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function emitError(
|
|
||||||
context: ParserContext,
|
|
||||||
type: ParserErrorTypes,
|
|
||||||
offset?: number
|
|
||||||
): void {
|
|
||||||
const loc = getCursor(context)
|
|
||||||
if (offset) {
|
|
||||||
loc.offset += offset
|
|
||||||
loc.column += offset
|
|
||||||
}
|
|
||||||
context.onError(type, loc)
|
|
||||||
}
|
|
||||||
|
|
||||||
function createParserContext(
|
function createParserContext(
|
||||||
content: string,
|
content: string,
|
||||||
options: ParserOptions
|
options: ParserOptions
|
||||||
@ -165,6 +77,11 @@ function createParserContext(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCursor(context: ParserContext): Position {
|
||||||
|
const { column, line, offset } = context
|
||||||
|
return { column, line, offset }
|
||||||
|
}
|
||||||
|
|
||||||
function parseChildren(
|
function parseChildren(
|
||||||
context: ParserContext,
|
context: ParserContext,
|
||||||
mode: TextModes,
|
mode: TextModes,
|
||||||
@ -255,6 +172,88 @@ function parseChildren(
|
|||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSelection(
|
||||||
|
context: ParserContext,
|
||||||
|
start: Position,
|
||||||
|
end?: Position
|
||||||
|
): SourceLocation {
|
||||||
|
end = end || getCursor(context)
|
||||||
|
return {
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
source: context.originalSource.slice(start.offset, end.offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function last<T>(xs: T[]): T | undefined {
|
||||||
|
return xs[xs.length - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
function startsWith(source: string, searchString: string): boolean {
|
||||||
|
return source.startsWith(searchString)
|
||||||
|
}
|
||||||
|
|
||||||
|
function advanceBy(context: ParserContext, numberOfCharacters: number): void {
|
||||||
|
__DEV__ && assert(numberOfCharacters <= context.source.length)
|
||||||
|
|
||||||
|
const { column, source } = context
|
||||||
|
const str = source.slice(0, numberOfCharacters)
|
||||||
|
const lines = str.split(/\r?\n/)
|
||||||
|
|
||||||
|
context.source = source.slice(numberOfCharacters)
|
||||||
|
context.offset += numberOfCharacters
|
||||||
|
context.line += lines.length - 1
|
||||||
|
context.column =
|
||||||
|
lines.length === 1
|
||||||
|
? column + numberOfCharacters
|
||||||
|
: Math.max(1, lines.pop()!.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
function advanceSpaces(context: ParserContext): void {
|
||||||
|
const match = /^[\t\r\n\f ]+/.exec(context.source)
|
||||||
|
if (match) {
|
||||||
|
advanceBy(context, match[0].length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNewPosition(
|
||||||
|
context: ParserContext,
|
||||||
|
start: Position,
|
||||||
|
numberOfCharacters: number
|
||||||
|
): Position {
|
||||||
|
const { originalSource } = context
|
||||||
|
const str = originalSource.slice(start.offset, numberOfCharacters)
|
||||||
|
const lines = str.split(/\r?\n/)
|
||||||
|
|
||||||
|
const newPosition = {
|
||||||
|
column: start.column,
|
||||||
|
line: start.line,
|
||||||
|
offset: start.offset
|
||||||
|
}
|
||||||
|
|
||||||
|
newPosition.offset += numberOfCharacters
|
||||||
|
newPosition.line += lines.length - 1
|
||||||
|
newPosition.column =
|
||||||
|
lines.length === 1
|
||||||
|
? start.column + numberOfCharacters
|
||||||
|
: Math.max(1, lines.pop()!.length)
|
||||||
|
|
||||||
|
return newPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
function emitError(
|
||||||
|
context: ParserContext,
|
||||||
|
type: ParserErrorTypes,
|
||||||
|
offset?: number
|
||||||
|
): void {
|
||||||
|
const loc = getCursor(context)
|
||||||
|
if (offset) {
|
||||||
|
loc.offset += offset
|
||||||
|
loc.column += offset
|
||||||
|
}
|
||||||
|
context.onError(type, loc)
|
||||||
|
}
|
||||||
|
|
||||||
function isEnd(
|
function isEnd(
|
||||||
context: ParserContext,
|
context: ParserContext,
|
||||||
mode: TextModes,
|
mode: TextModes,
|
||||||
@ -918,3 +917,9 @@ const CCR_REPLACEMENTS: { [key: number]: number | undefined } = {
|
|||||||
0x9e: 0x017e,
|
0x9e: 0x017e,
|
||||||
0x9f: 0x0178
|
0x9f: 0x0178
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function assert(condition: boolean, msg?: string) {
|
||||||
|
if (!condition) {
|
||||||
|
throw new Error(msg || `unexpected parser condition`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { TextModes, ParserOptions } from './parser'
|
import { TextModes, ParserOptions } from './parser'
|
||||||
import { ElementNode, Namespaces, Position, Node } from './ast'
|
import { ElementNode, Namespaces, Position, Node } from './ast'
|
||||||
import { ParserErrorTypes } from './errorTypes'
|
import { ParserErrorTypes } from './parserErrorTypes'
|
||||||
|
|
||||||
export const parserOptionsMinimal: ParserOptions = {
|
export const parserOptionsMinimal: ParserOptions = {
|
||||||
delimiters: [`{{`, `}}`],
|
delimiters: [`{{`, `}}`],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user