diff options
Diffstat (limited to 'src/compiler/parse/read')
-rw-r--r-- | src/compiler/parse/read/context.ts | 124 | ||||
-rw-r--r-- | src/compiler/parse/read/expression.ts | 67 | ||||
-rw-r--r-- | src/compiler/parse/read/script.ts | 87 |
3 files changed, 137 insertions, 141 deletions
diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts index 496d24bb1..4d8f12060 100644 --- a/src/compiler/parse/read/context.ts +++ b/src/compiler/parse/read/context.ts @@ -3,82 +3,70 @@ 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 { 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; +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 - }; - } + 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' - }); - } + 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; + 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; - } + 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; + 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); + 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); - } + 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 index 3af2868e6..56e79265d 100644 --- a/src/compiler/parse/read/expression.ts +++ b/src/compiler/parse/read/expression.ts @@ -6,36 +6,39 @@ 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); - } + 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 index c5fbee699..eb7a8c5b3 100644 --- a/src/compiler/parse/read/script.ts +++ b/src/compiler/parse/read/script.ts @@ -8,48 +8,53 @@ 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; + 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 - }; + 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, + }; } |