diff options
Diffstat (limited to 'src/parser/parse/read')
-rw-r--r-- | src/parser/parse/read/context.ts | 72 | ||||
-rw-r--r-- | src/parser/parse/read/expression.ts | 42 | ||||
-rw-r--r-- | src/parser/parse/read/script.ts | 61 | ||||
-rw-r--r-- | src/parser/parse/read/style.ts | 40 |
4 files changed, 215 insertions, 0 deletions
diff --git a/src/parser/parse/read/context.ts b/src/parser/parse/read/context.ts new file mode 100644 index 000000000..4d8f12060 --- /dev/null +++ b/src/parser/parse/read/context.ts @@ -0,0 +1,72 @@ +// @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/parser/parse/read/expression.ts b/src/parser/parse/read/expression.ts new file mode 100644 index 000000000..f691f4772 --- /dev/null +++ b/src/parser/parse/read/expression.ts @@ -0,0 +1,42 @@ +import { parse_expression_at } from '../acorn.js'; +import { Parser } from '../index.js'; +import { whitespace } from '../../utils/patterns.js'; +// import { Node } from 'estree'; + +// @ts-ignore +export default function read_expression(parser: Parser): string { + try { + const start = parser.index; + let index = parse_expression_at(parser.template, parser.index); + let num_parens = 0; + + for (let i = parser.index; i < start; i += 1) { + if (parser.template[i] === '(') num_parens += 1; + } + + 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(start, index); + // return node as Node; + } catch (err) { + parser.acorn_error(err); + } +}
\ No newline at end of file diff --git a/src/parser/parse/read/script.ts b/src/parser/parse/read/script.ts new file mode 100644 index 000000000..7afbfb08f --- /dev/null +++ b/src/parser/parse/read/script.ts @@ -0,0 +1,61 @@ +// @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): 'runtime' | 'setup' { + const context = attributes.find((attribute) => attribute.name === 'astro'); + if (!context) return 'runtime'; + if (context.value === true) return 'setup'; + + if (context.value.length !== 1 || context.value[0].type !== 'Text') { + parser.error( + { + code: 'invalid-script', + message: 'astro attribute must be static', + }, + start + ); + } + + const value = context.value[0].data; + + if (value !== 'setup') { + parser.error( + { + code: 'invalid-script', + message: 'If the "astro" attribute has a value, its value must be "setup"', + }, + 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/parser/parse/read/style.ts b/src/parser/parse/read/style.ts new file mode 100644 index 000000000..f23d7b10e --- /dev/null +++ b/src/parser/parse/read/style.ts @@ -0,0 +1,40 @@ +import { Parser } from '../index.js'; +import { Style } from '../../interfaces.js'; + +interface Attribute { + start: number; + end: number; + type: 'Attribute'; + name: string; + value: { + raw: string; + data: string; + }[]; +} + +export default function read_style(parser: Parser, start: number, attributes: Attribute[]): 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'; +} |