Home / Function/ parse() — tailwindcss Function Reference

parse() — tailwindcss Function Reference

Architecture documentation for the parse() function in css-parser.ts from the tailwindcss codebase.

Function typescript OxideCore Extractor calls 5 called by 10

Entity Profile

Dependency Diagram

graph TD
  253418a1_4f08_cf0e_5b8e_c6392b9959eb["parse()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9["rewriteUrls()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  9b0f6215_d728_39c5_c24a_2e50640933f4["expand()"]
  9b0f6215_d728_39c5_c24a_2e50640933f4 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748["compile()"]
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  fb5a2bcd_8ed3_9973_b6d8_b4f47715ef8e["__unstable__loadDesignSystem()"]
  fb5a2bcd_8ed3_9973_b6d8_b4f47715ef8e -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  5898a47f_729b_b30c_bf7b_865876b2750a["analyze()"]
  5898a47f_729b_b30c_bf7b_865876b2750a -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  a6e11c3d_c962_0a65_d91f_6fbe955cf4f0["optimizeAst()"]
  a6e11c3d_c962_0a65_d91f_6fbe955cf4f0 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  65cd5723_5f2b_da42_e83b_2add05ebc004["substituteAtImports()"]
  65cd5723_5f2b_da42_e83b_2add05ebc004 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  3b6a2079_7f12_42cd_ba9f_a57ecec4366d["buildPluginApi()"]
  3b6a2079_7f12_42cd_ba9f_a57ecec4366d -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  c0d9b918_4b8c_8efd_d0a2_c73e574b0ebc["parseVariantValue()"]
  c0d9b918_4b8c_8efd_d0a2_c73e574b0ebc -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  387b2b72_8509_7339_6444_0aa98e0581bb["replaceNestedClassNameReferences()"]
  387b2b72_8509_7339_6444_0aa98e0581bb -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  f4c1885c_39a0_4343_f87c_7a8b1145a3bc["comment()"]
  253418a1_4f08_cf0e_5b8e_c6392b9959eb -->|calls| f4c1885c_39a0_4343_f87c_7a8b1145a3bc
  e44fc50a_a1a4_b875_1354_734ef5f055f5["parseString()"]
  253418a1_4f08_cf0e_5b8e_c6392b9959eb -->|calls| e44fc50a_a1a4_b875_1354_734ef5f055f5
  3f582344_0fd2_bfec_9031_5c3223919ed1["parseDeclaration()"]
  253418a1_4f08_cf0e_5b8e_c6392b9959eb -->|calls| 3f582344_0fd2_bfec_9031_5c3223919ed1
  1d825a09_87b5_c531_bb8e_a4c358c7b855["parseAtRule()"]
  253418a1_4f08_cf0e_5b8e_c6392b9959eb -->|calls| 1d825a09_87b5_c531_bb8e_a4c358c7b855
  style 253418a1_4f08_cf0e_5b8e_c6392b9959eb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/css-parser.ts lines 64–594

export function parse(input: string, opts?: ParseOptions) {
  let source: Source | null = opts?.from ? { file: opts.from, code: input } : null

  // Note: it is important that any transformations of the input string
  // *before* processing do NOT change the length of the string. This
  // would invalidate the mechanism used to track source locations.
  if (input[0] === '\uFEFF') input = ' ' + input.slice(1)

  let ast: AstNode[] = []
  let licenseComments: Comment[] = []

  let stack: (Rule | null)[] = []

  let parent = null as Rule | null
  let node = null as AstNode | null

  let buffer = ''
  let closingBracketStack = ''

  // The start of the first non-whitespace character in the buffer
  let bufferStart = 0

  let peekChar

  for (let i = 0; i < input.length; i++) {
    let currentChar = input.charCodeAt(i)

    // Skip over the CR in CRLF. This allows code below to only check for a line
    // break even if we're looking at a Windows newline. Peeking the input still
    // has to check for CRLF but that happens less often.
    if (currentChar === CARRIAGE_RETURN) {
      peekChar = input.charCodeAt(i + 1)
      if (peekChar === LINE_BREAK) continue
    }

    // Current character is a `\` therefore the next character is escaped,
    // consume it together with the next character and continue.
    //
    // E.g.:
    //
    // ```css
    // .hover\:foo:hover {}
    //       ^
    // ```
    //
    if (currentChar === BACKSLASH) {
      if (buffer === '') bufferStart = i
      buffer += input.slice(i, i + 2)
      i += 1
    }

    // Start of a comment.
    //
    // E.g.:
    //
    // ```css
    // /* Example */
    // ^^^^^^^^^^^^^
    // .foo {
    //  color: red; /* Example */
    //              ^^^^^^^^^^^^^
    // }
    // .bar {
    //  color: /* Example */ red;
    //         ^^^^^^^^^^^^^
    // }
    // ```
    else if (currentChar === SLASH && input.charCodeAt(i + 1) === ASTERISK) {
      let start = i

      for (let j = i + 2; j < input.length; j++) {
        peekChar = input.charCodeAt(j)

        // Current character is a `\` therefore the next character is escaped.
        if (peekChar === BACKSLASH) {
          j += 1
        }

        // End of the comment
        else if (peekChar === ASTERISK && input.charCodeAt(j + 1) === SLASH) {
          i = j + 1
          break
        }
      }

      let commentString = input.slice(start, i + 1)

      // Collect all license comments so that we can hoist them to the top of
      // the AST.
      if (commentString.charCodeAt(2) === EXCLAMATION_MARK) {
        let node = comment(commentString.slice(2, -2))
        licenseComments.push(node)

        if (source) {
          node.src = [source, start, i + 1]
          node.dst = [source, start, i + 1]
        }
      }
    }

    // Start of a string.
    else if (currentChar === SINGLE_QUOTE || currentChar === DOUBLE_QUOTE) {
      let end = parseString(input, i, currentChar, source)

      // Adjust `buffer` to include the string.
      buffer += input.slice(i, end + 1)
      i = end
    }

    // Skip whitespace if the next character is also whitespace. This allows us
    // to reduce the amount of whitespace in the AST.
    else if (
      (currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB) &&
      (peekChar = input.charCodeAt(i + 1)) &&
      (peekChar === SPACE ||
        peekChar === LINE_BREAK ||
        peekChar === TAB ||
        (peekChar === CARRIAGE_RETURN &&
          (peekChar = input.charCodeAt(i + 2)) &&
          peekChar == LINE_BREAK))
    ) {
      continue
    }

    // Replace new lines with spaces.
    else if (currentChar === LINE_BREAK) {
      if (buffer.length === 0) continue

      peekChar = buffer.charCodeAt(buffer.length - 1)
      if (peekChar !== SPACE && peekChar !== LINE_BREAK && peekChar !== TAB) {
        buffer += ' '
      }
    }

    // Start of a custom property.
    //
    // Custom properties are very permissive and can contain almost any
    // character, even `;` and `}`. Therefore we have to make sure that we are
    // at the correct "end" of the custom property by making sure everything is
    // balanced.
    else if (currentChar === DASH && input.charCodeAt(i + 1) === DASH && buffer.length === 0) {
      let closingBracketStack = ''

      let start = i
      let colonIdx = -1

      for (let j = i + 2; j < input.length; j++) {
        peekChar = input.charCodeAt(j)

        // Current character is a `\` therefore the next character is escaped.
        if (peekChar === BACKSLASH) {
          j += 1
        }

        // Start of a string.
        else if (peekChar === SINGLE_QUOTE || peekChar === DOUBLE_QUOTE) {
          j = parseString(input, j, peekChar, source)
        }

        // Start of a comment.
        else if (peekChar === SLASH && input.charCodeAt(j + 1) === ASTERISK) {
          for (let k = j + 2; k < input.length; k++) {
            peekChar = input.charCodeAt(k)
            // Current character is a `\` therefore the next character is escaped.
            if (peekChar === BACKSLASH) {
              k += 1
            }

            // End of the comment
            else if (peekChar === ASTERISK && input.charCodeAt(k + 1) === SLASH) {
              j = k + 1
              break
            }
          }
        }

        // End of the "property" of the property-value pair.
        else if (colonIdx === -1 && peekChar === COLON) {
          colonIdx = buffer.length + j - start
        }

        // End of the custom property.
        else if (peekChar === SEMICOLON && closingBracketStack.length === 0) {
          buffer += input.slice(start, j)
          i = j
          break
        }

        // Start of a block.
        else if (peekChar === OPEN_PAREN) {
          closingBracketStack += ')'
        } else if (peekChar === OPEN_BRACKET) {
          closingBracketStack += ']'
        } else if (peekChar === OPEN_CURLY) {
          closingBracketStack += '}'
        }

        // End of the custom property if didn't use a `;` to end the custom
        // property.
        //
        // E.g.:
        //
        // ```css
        // .foo {
        //   --custom: value
        //                  ^
        // }
        // ```
        else if (
          (peekChar === CLOSE_CURLY || input.length - 1 === j) &&
          closingBracketStack.length === 0
        ) {
          i = j - 1
          buffer += input.slice(start, j)
          break
        }

        // End of a block.
        else if (
          peekChar === CLOSE_PAREN ||
          peekChar === CLOSE_BRACKET ||
          peekChar === CLOSE_CURLY
        ) {
          if (
            closingBracketStack.length > 0 &&
            input[j] === closingBracketStack[closingBracketStack.length - 1]
          ) {
            closingBracketStack = closingBracketStack.slice(0, -1)
          }
        }
      }

      let declaration = parseDeclaration(buffer, colonIdx)
      if (!declaration) {
        throw new CssSyntaxError(
          `Invalid custom property, expected a value`,
          source ? [source, start, i] : null,
        )
      }

      if (source) {
        declaration.src = [source, start, i]
        declaration.dst = [source, start, i]
      }

      if (parent) {
        parent.nodes.push(declaration)
      } else {
        ast.push(declaration)
      }

      buffer = ''
    }

    // End of a body-less at-rule.
    //
    // E.g.:
    //
    // ```css
    // @charset "UTF-8";
    //                 ^
    // ```
    else if (currentChar === SEMICOLON && buffer.charCodeAt(0) === AT_SIGN) {
      node = parseAtRule(buffer)

      if (source) {
        node.src = [source, bufferStart, i]
        node.dst = [source, bufferStart, i]
      }

      // At-rule is nested inside of a rule, attach it to the parent.
      if (parent) {
        parent.nodes.push(node)
      }

      // We are the root node which means we are done with the current node.
      else {
        ast.push(node)
      }

      // Reset the state for the next node.
      buffer = ''
      node = null
    }

    // End of a declaration.
    //
    // E.g.:
    //
    // ```css
    // .foo {
    //   color: red;
    //             ^
    // }
    // ```
    //
    else if (
      currentChar === SEMICOLON &&
      closingBracketStack[closingBracketStack.length - 1] !== ')'
    ) {
      let declaration = parseDeclaration(buffer)
      if (!declaration) {
        if (buffer.length === 0) continue
        throw new CssSyntaxError(
          `Invalid declaration: \`${buffer.trim()}\``,
          source ? [source, bufferStart, i] : null,
        )
      }

      if (source) {
        declaration.src = [source, bufferStart, i]
        declaration.dst = [source, bufferStart, i]
      }

      if (parent) {
        parent.nodes.push(declaration)
      } else {
        ast.push(declaration)
      }

      buffer = ''
    }

    // Start of a block.
    else if (
      currentChar === OPEN_CURLY &&
      closingBracketStack[closingBracketStack.length - 1] !== ')'
    ) {
      closingBracketStack += '}'

      // At this point `buffer` should resemble a selector or an at-rule.
      node = rule(buffer.trim())

      // Track the source location for source maps
      if (source) {
        node.src = [source, bufferStart, i]
        node.dst = [source, bufferStart, i]
      }

      // Attach the rule to the parent in case it's nested.
      if (parent) {
        parent.nodes.push(node)
      }

      // Push the parent node to the stack, so that we can go back once the
      // nested nodes are done.
      stack.push(parent)

      // Make the current node the new parent, so that nested nodes can be
      // attached to it.
      parent = node

      // Reset the state for the next node.
      buffer = ''
      node = null
    }

    // End of a block.
    else if (
      currentChar === CLOSE_CURLY &&
      closingBracketStack[closingBracketStack.length - 1] !== ')'
    ) {
      if (closingBracketStack === '') {
        throw new CssSyntaxError('Missing opening {', source ? [source, i, i] : null)
      }

      closingBracketStack = closingBracketStack.slice(0, -1)

      // When we hit a `}` and `buffer` is filled in, then it means that we did
      // not complete the previous node yet. This means that we hit a
      // declaration without a `;` at the end.
      if (buffer.length > 0) {
        // This can happen for nested at-rules.
        //
        // E.g.:
        //
        // ```css
        // @layer foo {
        //   @tailwind utilities
        //                      ^
        // }
        // ```
        if (buffer.charCodeAt(0) === AT_SIGN) {
          node = parseAtRule(buffer)

          // Track the source location for source maps
          if (source) {
            node.src = [source, bufferStart, i]
            node.dst = [source, bufferStart, i]
          }

          // At-rule is nested inside of a rule, attach it to the parent.
          if (parent) {
            parent.nodes.push(node)
          }

          // We are the root node which means we are done with the current node.
          else {
            ast.push(node)
          }

          // Reset the state for the next node.
          buffer = ''
          node = null
        }

        // But it can also happen for declarations.
        //
        // E.g.:
        //
        // ```css
        // .foo {
        //   color: red
        //             ^
        // }
        // ```
        else {
          // Split `buffer` into a `property` and a `value`. At this point the
          // comments are already removed which means that we don't have to worry
          // about `:` inside of comments.
          let colonIdx = buffer.indexOf(':')

          // Attach the declaration to the parent.
          if (parent) {
            let node = parseDeclaration(buffer, colonIdx)
            if (!node) {
              throw new CssSyntaxError(
                `Invalid declaration: \`${buffer.trim()}\``,
                source ? [source, bufferStart, i] : null,
              )
            }

            if (source) {
              node.src = [source, bufferStart, i]
              node.dst = [source, bufferStart, i]
            }

            parent.nodes.push(node)
          }
        }
      }

      // We are done with the current node, which means we can go up one level
      // in the stack.
      let grandParent = stack.pop() ?? null

      // We are the root node which means we are done and continue with the next
      // node.
      if (grandParent === null && parent) {
        ast.push(parent)
      }

      // Go up one level in the stack.
      parent = grandParent

      // Reset the state for the next node.
      buffer = ''
      node = null
    }

    // `(`
    else if (currentChar === OPEN_PAREN) {
      closingBracketStack += ')'
      buffer += '('
    }

    // `)`
    else if (currentChar === CLOSE_PAREN) {
      if (closingBracketStack[closingBracketStack.length - 1] !== ')') {
        throw new CssSyntaxError('Missing opening (', source ? [source, i, i] : null)
      }

      closingBracketStack = closingBracketStack.slice(0, -1)
      buffer += ')'
    }

    // Any other character is part of the current node.
    else {
      // Skip whitespace at the start of a new node.
      if (
        buffer.length === 0 &&
        (currentChar === SPACE || currentChar === LINE_BREAK || currentChar === TAB)
      ) {
        continue
      }

      if (buffer === '') bufferStart = i

      buffer += String.fromCharCode(currentChar)
    }
  }

  // If we have a leftover `buffer` that happens to start with an `@` then it
  // means that we have an at-rule that is not terminated with a semicolon at
  // the end of the input.
  if (buffer.charCodeAt(0) === AT_SIGN) {
    let node = parseAtRule(buffer)

    // Track the source location for source maps
    if (source) {
      node.src = [source, bufferStart, input.length]
      node.dst = [source, bufferStart, input.length]
    }

    ast.push(node)
  }

  // When we are done parsing then everything should be balanced. If we still
  // have a leftover `parent`, then it means that we have an unterminated block.
  if (closingBracketStack.length > 0 && parent) {
    if (parent.kind === 'rule') {
      throw new CssSyntaxError(
        `Missing closing } at ${parent.selector}`,
        parent.src ? [parent.src[0], parent.src[1], parent.src[1]] : null,
      )
    }

    if (parent.kind === 'at-rule') {
      throw new CssSyntaxError(
        `Missing closing } at ${parent.name} ${parent.params}`,
        parent.src ? [parent.src[0], parent.src[1], parent.src[1]] : null,
      )
    }
  }

  if (licenseComments.length > 0) {
    return (licenseComments as AstNode[]).concat(ast)
  }

  return ast
}

Domain

Subdomains

Frequently Asked Questions

What does parse() do?
parse() is a function in the tailwindcss codebase.
What does parse() call?
parse() calls 5 function(s): comment, parseAtRule, parseDeclaration, parseString, rule.
What calls parse()?
parse() is called by 10 function(s): __unstable__loadDesignSystem, analyze, buildPluginApi, compile, expand, optimizeAst, parseVariantValue, replaceNestedClassNameReferences, and 2 more.

Analyze Your Own Codebase

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

Try Supermodel Free