summaryrefslogtreecommitdiff
path: root/src/compiler/parse/read
diff options
context:
space:
mode:
authorGravatar Fred K. Schott <fkschott@gmail.com> 2021-03-25 00:00:22 -0700
committerGravatar GitHub <noreply@github.com> 2021-03-25 00:00:22 -0700
commit30cccdf7154b6470e876464da9e412af10894dd5 (patch)
tree73ed40b30af23ba3e5b94070e478f3e2ca1670c0 /src/compiler/parse/read
parenta72ab10c623022860691d6a095b74dea70cc6f69 (diff)
downloadastro-30cccdf7154b6470e876464da9e412af10894dd5.tar.gz
astro-30cccdf7154b6470e876464da9e412af10894dd5.tar.zst
astro-30cccdf7154b6470e876464da9e412af10894dd5.zip
add component state, top-level await support (#26)
Diffstat (limited to 'src/compiler/parse/read')
-rw-r--r--src/compiler/parse/read/context.ts72
-rw-r--r--src/compiler/parse/read/expression.ts42
-rw-r--r--src/compiler/parse/read/script.ts61
-rw-r--r--src/compiler/parse/read/style.ts40
4 files changed, 0 insertions, 215 deletions
diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts
deleted file mode 100644
index 4d8f12060..000000000
--- a/src/compiler/parse/read/context.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-// @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
deleted file mode 100644
index 1fe5f05f1..000000000
--- a/src/compiler/parse/read/expression.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-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);
- }
-}
diff --git a/src/compiler/parse/read/script.ts b/src/compiler/parse/read/script.ts
deleted file mode 100644
index 7afbfb08f..000000000
--- a/src/compiler/parse/read/script.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-// @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/compiler/parse/read/style.ts b/src/compiler/parse/read/style.ts
deleted file mode 100644
index f23d7b10e..000000000
--- a/src/compiler/parse/read/style.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-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';
-}