{"id":21654657,"url":"https://github.com/localvoid/tagged-template-scanner","last_synced_at":"2025-03-20T04:42:01.968Z","repository":{"id":42828962,"uuid":"266123076","full_name":"localvoid/tagged-template-scanner","owner":"localvoid","description":null,"archived":false,"fork":false,"pushed_at":"2022-03-26T18:31:26.000Z","size":134,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T18:51:30.286Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/localvoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-22T13:55:03.000Z","updated_at":"2022-09-08T14:47:04.000Z","dependencies_parsed_at":"2022-09-19T20:53:56.339Z","dependency_job_id":null,"html_url":"https://github.com/localvoid/tagged-template-scanner","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Ftagged-template-scanner","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Ftagged-template-scanner/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Ftagged-template-scanner/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/localvoid%2Ftagged-template-scanner/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/localvoid","download_url":"https://codeload.github.com/localvoid/tagged-template-scanner/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244554121,"owners_count":20471173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T08:28:37.810Z","updated_at":"2025-03-20T04:42:01.950Z","avatar_url":"https://github.com/localvoid.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tagged Template Scanner\n\n## Usage Example\n\nSimple HTML-like parser\n\n```ts\nimport * as s from \"tagged-template-scanner\";\n\nexport interface TElement {\n  readonly type: \"element\";\n  readonly tagName: string;\n  readonly attributes: TAttribute[];\n  readonly children: (TText | TElement | TExpression | null)[];\n}\n\nexport interface TExpression {\n  readonly type: \"expression\";\n  readonly index: number;\n}\n\nexport interface TText {\n  readonly type: \"text\";\n  readonly text: string;\n}\n\nexport interface TAttribute {\n  readonly type: \"attribute\";\n  readonly key: string;\n  readonly value: undefined | TText | TExpression;\n}\n\nexport type TNode = TExpression | TAttribute | TElement | TText;\n\nexport class ParserError extends Error {\n  readonly staticIndex: number;\n  readonly textIndex: number;\n\n  constructor(message: string, staticIndex: number, textIndex: number) {\n    super(message);\n    this.staticIndex = staticIndex;\n    this.textIndex = textIndex;\n  }\n}\n\nexport function parseTemplate(statics: string[] | TemplateStringsArray): TElement {\n  try {\n    const scanner = s.createTaggedTemplateScanner(statics);\n    s.consumeWhitespace(scanner);\n    return parseElement(scanner);\n  } catch (e) {\n    if (e instanceof ParserError) {\n      throw Error(s.formatError(statics, e.message, s.calcOffset(statics, e.staticIndex, e.textIndex)));\n    }\n    throw e;\n  }\n}\n\nconst IDENTIFIER = /[a-zA-Z][0-9a-zA-Z-_]*/g;\nconst ATTRIBUTE_VALUE = /[^\"]*/g;\n\nfunction parseElement(scanner: s.TaggedTemplateScanner): TElement {\n  if (!s.consumeCharCode(scanner, s.CharCode.LessThan)) {\n    throw error(scanner, `Invalid element, expected '\u003c'`);\n  }\n  const tagName = s.consumeRegExp(scanner, IDENTIFIER);\n  if (!tagName) {\n    throw error(scanner, `Invalid element, expected element tag name`);\n  }\n\n  const attributes = parseAttributes(scanner);\n  if (s.consumeCharCode(scanner, s.CharCode.Slash)) {\n    if (!s.consumeCharCode(scanner, s.CharCode.MoreThan)) {\n      throw error(scanner, `Invalid element, expected '\u003e'`);\n    }\n    return { type: \"element\", tagName, attributes, children: [] };\n  }\n  if (!s.consumeCharCode(scanner, s.CharCode.MoreThan)) {\n    throw error(scanner, `Invalid element, expected '\u003e'`);\n  }\n\n  const children = parseElementChildren(scanner);\n\n  if (!s.consumeCharCode(scanner, s.CharCode.LessThan)) {\n    throw error(scanner, `Invalid element, expected '\u003c'`);\n  }\n  if (!s.consumeCharCode(scanner, s.CharCode.Slash)) {\n    throw error(scanner, `Invalid element, expected '/'`);\n  }\n  if (!s.consumeCharCode(scanner, s.CharCode.MoreThan)) {\n    if (!s.consumeString(scanner, tagName)) {\n      throw error(scanner, `Invalid element, expected tag name '${tagName}'`);\n    }\n    if (!s.consumeCharCode(scanner, s.CharCode.MoreThan)) {\n      throw error(scanner, `Invalid element, expected '\u003e'`);\n    }\n  }\n\n  return { type: \"element\", tagName, attributes, children };\n}\n\nfunction parseElementChildren(scanner: s.TaggedTemplateScanner): (TElement | TExpression | TText)[] {\n  const children: (TElement | TExpression | TText)[] = [];\n  let c;\n  s.consumeWhitespace(scanner);\n  while (!s.isEnd(scanner)) {\n    if (s.matchString(scanner, \"\u003c/\")) {\n      return children;\n    }\n    if (c = parseNode(scanner)) {\n      children.push(c);\n    }\n  }\n  throw error(scanner, `Invalid element children list, unexpected end of template`);\n}\n\nfunction parseNode(scanner: s.TaggedTemplateScanner): TElement | TExpression | TText | null {\n  let index;\n  let text;\n  if (s.isEnd(scanner)) {\n    throw error(scanner, `Invalid node, unexpected end of template`);\n  }\n  if ((index = s.consumeExpr(scanner)) \u003e= 0) {\n    return { type: \"expression\", index };\n  }\n  if (s.peekCharCode(scanner) === s.CharCode.LessThan) {\n    return parseElement(scanner);\n  }\n  if (text = parseText(scanner)) {\n    return text;\n  }\n  return null;\n}\n\nfunction parseText(scanner: s.TaggedTemplateScanner): TText | null {\n  let chars: number[] = [];\n  let text: string;\n  let c: number;\n\n  while (true) {\n    if (s.isEnd(scanner)) {\n      throw error(scanner, `Invalid text, unexpected end of template`);\n    }\n    if (s.isExpr(scanner) || (c = s.peekCharCode(scanner)) === s.CharCode.LessThan) {\n      break;\n    }\n\n    chars.push(c);\n    scanner.i++;\n  }\n\n  if (chars.length \u003e 0) {\n    text = String.fromCharCode.apply(void 0, chars);\n    if (text.length \u003e 0) {\n      return { type: \"text\", text };\n    }\n  }\n  return null;\n}\n\nfunction parseAttributes(scanner: s.TaggedTemplateScanner): TAttribute[] {\n  const properties: TAttribute[] = [];\n\n  s.consumeWhitespace(scanner);\n  let c = s.peekCharCode(scanner);\n  if (c === s.CharCode.Slash || c === s.CharCode.MoreThan) {\n    return properties;\n  }\n\n  while (true) {\n    if (s.isEnd(scanner)) {\n      throw error(scanner, `Invalid element properties, unexpected end of template`);\n    }\n\n    properties.push(parseAttribute(scanner));\n\n    c = s.peekCharCode(scanner);\n    if (c === s.CharCode.Slash || c === s.CharCode.MoreThan) {\n      return properties;\n    }\n\n    if (!s.isWhitespace(c)) {\n      throw error(scanner, `Invalid element properties, expected whitespace`);\n    }\n    s.consumeWhitespace(scanner);\n  }\n}\n\nfunction parseAttribute(scanner: s.TaggedTemplateScanner): TAttribute {\n  const key = s.consumeRegExp(scanner, IDENTIFIER);\n  if (!key) {\n    throw error(scanner, `Invalid attribute, expected attribute name`);\n  }\n  if (s.consumeCharCode(scanner, s.CharCode.EqualsTo)) {\n    return { type: \"attribute\", key, value: parseAttributeValue(scanner) };\n  }\n\n  return { type: \"attribute\", key, value: void 0 };\n}\n\nfunction parseAttributeValue(scanner: s.TaggedTemplateScanner): TText | TExpression {\n  const index = s.consumeExpr(scanner);\n  if (index \u003e= 0) {\n    return { type: \"expression\", index };\n  }\n\n  if (!s.consumeCharCode(scanner, s.CharCode.DoubleQuote)) {\n    throw error(scanner, `Invalid attribute value, expected '\"'`);\n  }\n\n  const text = s.consumeRegExp(scanner, ATTRIBUTE_VALUE);\n  if (!text || !s.consumeCharCode(scanner, s.CharCode.DoubleQuote)) {\n    throw error(scanner, `Invalid attribute value, expected '\"'`);\n  }\n\n  return { type: \"text\", text };\n}\n\nfunction error(scanner: s.TaggedTemplateScanner, message: string): ParserError {\n  return new ParserError(message, scanner.s, scanner.i);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalvoid%2Ftagged-template-scanner","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flocalvoid%2Ftagged-template-scanner","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flocalvoid%2Ftagged-template-scanner/lists"}