Home / Class/ Parser Class — svelte Architecture

Parser Class — svelte Architecture

Architecture documentation for the Parser class in index.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d["Parser"]
  67d7170b_725b_33cc_868d_fad6ed435252["index.js"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|defined in| 67d7170b_725b_33cc_868d_fad6ed435252
  2eaeae66_e151_7496_a156_059b566d630d["forCss()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 2eaeae66_e151_7496_a156_059b566d630d
  3220db43_dde7_5166_5db8_db3c6d68258d["constructor()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 3220db43_dde7_5166_5db8_db3c6d68258d
  3cddfbc5_2f69_6147_3f68_deab3d5175dd["current()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 3cddfbc5_2f69_6147_3f68_deab3d5175dd
  45588bdb_35e3_419d_0abe_bb334afe5127["acorn_error()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 45588bdb_35e3_419d_0abe_bb334afe5127
  0a126ed1_9325_909c_db03_54fdcc1d99b4["eat()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 0a126ed1_9325_909c_db03_54fdcc1d99b4
  08102023_91f0_7c64_a1a5_85f0bca37d85["match()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 08102023_91f0_7c64_a1a5_85f0bca37d85
  05c744ae_19fc_c5f7_194c_17e9aefcd3a7["match_regex()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 05c744ae_19fc_c5f7_194c_17e9aefcd3a7
  950ca39f_31ab_347e_80f0_e8d6443d0398["allow_whitespace()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 950ca39f_31ab_347e_80f0_e8d6443d0398
  cfd99184_1ff9_92b3_9acb_e17038f0a9e4["read()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| cfd99184_1ff9_92b3_9acb_e17038f0a9e4
  f1ad3daf_8116_e789_0965_794a1b45544d["read_identifier()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| f1ad3daf_8116_e789_0965_794a1b45544d
  a21e34c1_babd_5944_d71e_54c4483ed94a["read_until()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| a21e34c1_babd_5944_d71e_54c4483ed94a
  199f7881_8752_bc52_50f9_f8d68e4043b7["require_whitespace()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 199f7881_8752_bc52_50f9_f8d68e4043b7
  383a4823_60d9_d399_2d48_c9bad8d5ed7f["pop()"]
  042e583c_5e12_9bde_ddd8_8091d5ea0f7d -->|method| 383a4823_60d9_d399_2d48_c9bad8d5ed7f

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/1-parse/index.js lines 20–306

export class Parser {
	/**
	 * @readonly
	 * @type {string}
	 */
	template;

	/**
	 * Whether or not we're in loose parsing mode, in which
	 * case we try to continue parsing as much as possible
	 * @type {boolean}
	 */
	loose;

	/** */
	index = 0;

	/**
	 * Creates a minimal parser instance for CSS-only parsing.
	 * Skips Svelte component parsing setup.
	 * @param {string} source
	 * @returns {Parser}
	 */
	static forCss(source) {
		const parser = Object.create(Parser.prototype);
		parser.template = source;
		parser.index = 0;
		parser.loose = false;
		return parser;
	}

	/** Whether we're parsing in TypeScript mode */
	ts = false;

	/** @type {AST.TemplateNode[]} */
	stack = [];

	/** @type {AST.Fragment[]} */
	fragments = [];

	/** @type {AST.Root} */
	root;

	/** @type {Record<string, boolean>} */
	meta_tags = {};

	/** @type {LastAutoClosedTag | undefined} */
	last_auto_closed_tag;

	/**
	 * @param {string} template
	 * @param {boolean} loose
	 */
	constructor(template, loose) {
		if (typeof template !== 'string') {
			throw new TypeError('Template must be a string');
		}

		this.loose = loose;
		this.template = template.trimEnd();

		let match_lang;

		do match_lang = regex_lang_attribute.exec(template);
		while (match_lang && match_lang[0][1] !== 's'); // ensure it starts with '<s' to match script tags

		regex_lang_attribute.lastIndex = 0; // reset matched index to pass tests - otherwise declare the regex inside the constructor

		this.ts = match_lang?.[2] === 'ts';

		this.root = {
			css: null,
			js: [],
			// @ts-ignore
			start: null,
			// @ts-ignore
			end: null,
			type: 'Root',
			fragment: create_fragment(),
			options: null,
			comments: [],

Domain

Frequently Asked Questions

What is the Parser class?
Parser is a class in the svelte codebase, defined in packages/svelte/src/compiler/phases/1-parse/index.js.
Where is Parser defined?
Parser is defined in packages/svelte/src/compiler/phases/1-parse/index.js at line 20.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free