diff options
Diffstat (limited to 'src/compiler/parse/read')
-rw-r--r-- | src/compiler/parse/read/context.ts | 84 | ||||
-rw-r--r-- | src/compiler/parse/read/expression.ts | 41 | ||||
-rw-r--r-- | src/compiler/parse/read/script.ts | 55 | ||||
-rw-r--r-- | src/compiler/parse/read/style.ts | 33 |
4 files changed, 213 insertions, 0 deletions
diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts new file mode 100644 index 000000000..496d24bb1 --- /dev/null +++ b/src/compiler/parse/read/context.ts @@ -0,0 +1,84 @@ +// @ts-nocheck + +import { Parser } from '../index.js'; +import { isIdentifierStart } from 'acorn'; +import full_char_code_at from '../../utils/full_char_code_at.js'; +import { + is_bracket_open, + is_bracket_close, + is_bracket_pair, + get_bracket_close +} from '../utils/bracket.js'; +import { parse_expression_at } from '../acorn.js'; +import { Pattern } from 'estree'; + +export default function read_context( + parser: Parser +): Pattern & { start: number; end: number } { + const start = parser.index; + let i = parser.index; + + const code = full_char_code_at(parser.template, i); + if (isIdentifierStart(code, true)) { + return { + type: 'Identifier', + name: parser.read_identifier(), + start, + end: parser.index + }; + } + + if (!is_bracket_open(code)) { + parser.error({ + code: 'unexpected-token', + message: 'Expected identifier or destructure pattern' + }); + } + + const bracket_stack = [code]; + i += code <= 0xffff ? 1 : 2; + + while (i < parser.template.length) { + const code = full_char_code_at(parser.template, i); + if (is_bracket_open(code)) { + bracket_stack.push(code); + } else if (is_bracket_close(code)) { + if (!is_bracket_pair(bracket_stack[bracket_stack.length - 1], code)) { + parser.error({ + code: 'unexpected-token', + message: `Expected ${String.fromCharCode( + get_bracket_close(bracket_stack[bracket_stack.length - 1]) + )}` + }); + } + bracket_stack.pop(); + if (bracket_stack.length === 0) { + i += code <= 0xffff ? 1 : 2; + break; + } + } + i += code <= 0xffff ? 1 : 2; + } + + parser.index = i; + + const pattern_string = parser.template.slice(start, i); + try { + // the length of the `space_with_newline` has to be start - 1 + // because we added a `(` in front of the pattern_string, + // which shifted the entire string to right by 1 + // so we offset it by removing 1 character in the `space_with_newline` + // to achieve that, we remove the 1st space encountered, + // so it will not affect the `column` of the node + let space_with_newline = parser.template.slice(0, start).replace(/[^\n]/g, ' '); + const first_space = space_with_newline.indexOf(' '); + space_with_newline = space_with_newline.slice(0, first_space) + space_with_newline.slice(first_space + 1); + + return (parse_expression_at( + `${space_with_newline}(${pattern_string} = 1)`, + start - 1 + ) as any).left; + } catch (error) { + parser.acorn_error(error); + } +} diff --git a/src/compiler/parse/read/expression.ts b/src/compiler/parse/read/expression.ts new file mode 100644 index 000000000..3af2868e6 --- /dev/null +++ b/src/compiler/parse/read/expression.ts @@ -0,0 +1,41 @@ +// @ts-nocheck + +import { parse_expression_at } from '../acorn.js'; +import { Parser } from '../index.js'; +import { whitespace } from '../../utils/patterns.js'; +// import { Node } from 'estree'; + +export default function read_expression(parser: Parser): string { + try { + const node = parse_expression_at(parser.template, parser.index); + + let num_parens = 0; + + for (let i = parser.index; i < node.start; i += 1) { + if (parser.template[i] === '(') num_parens += 1; + } + + let index = node.end; + while (num_parens > 0) { + const char = parser.template[index]; + + if (char === ')') { + num_parens -= 1; + } else if (!whitespace.test(char)) { + parser.error({ + code: 'unexpected-token', + message: 'Expected )' + }, index); + } + + index += 1; + } + + parser.index = index; + + return parser.template.substring(node.start, node.end); + // return node as Node; + } catch (err) { + parser.acorn_error(err); + } +} diff --git a/src/compiler/parse/read/script.ts b/src/compiler/parse/read/script.ts new file mode 100644 index 000000000..c5fbee699 --- /dev/null +++ b/src/compiler/parse/read/script.ts @@ -0,0 +1,55 @@ +// @ts-nocheck + +import * as acorn from '../acorn'; +import { Parser } from '../index.js'; +import { Script } from '../../interfaces.js'; +import { Node, Program } from 'estree'; + +const script_closing_tag = '</script>'; + +function get_context(parser: Parser, attributes: any[], start: number): string { + const context = attributes.find(attribute => attribute.name === 'context'); + if (!context) return 'default'; + + if (context.value.length !== 1 || context.value[0].type !== 'Text') { + parser.error({ + code: 'invalid-script', + message: 'context attribute must be static' + }, start); + } + + const value = context.value[0].data; + + if (value !== 'module') { + parser.error({ + code: 'invalid-script', + message: 'If the context attribute is supplied, its value must be "module"' + }, context.start); + } + + return value; +} + +export default function read_script(parser: Parser, start: number, attributes: Node[]): Script { + const script_start = parser.index; + const script_end = parser.template.indexOf(script_closing_tag, script_start); + + if (script_end === -1) { + parser.error({ + code: 'unclosed-script', + message: '<script> must have a closing tag' + }); + } + + const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + + parser.template.slice(script_start, script_end); + parser.index = script_end + script_closing_tag.length; + + return { + type: 'Script', + start, + end: parser.index, + context: get_context(parser, attributes, start), + content: source + }; +} diff --git a/src/compiler/parse/read/style.ts b/src/compiler/parse/read/style.ts new file mode 100644 index 000000000..deb60de46 --- /dev/null +++ b/src/compiler/parse/read/style.ts @@ -0,0 +1,33 @@ +import { Parser } from '../index.js'; +import { Node } from 'estree'; +import { Style } from '../../interfaces.js'; + +export default function read_style(parser: Parser, start: number, attributes: Node[]): Style { + const content_start = parser.index; + const styles = parser.read_until(/<\/style>/); + const content_end = parser.index; + parser.eat('</style>', true); + const end = parser.index; + + return { + type: 'Style', + start, + end, + attributes, + content: { + start: content_start, + end: content_end, + styles + } + }; +} + +function is_ref_selector(a: any, b: any) { // TODO add CSS node types + if (!b) return false; + + return ( + a.type === 'TypeSelector' && + a.name === 'ref' && + b.type === 'PseudoClassSelector' + ); +} |