Home / Function/ parse() — tailwindcss Function Reference

parse() — tailwindcss Function Reference

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

Function typescript OxideCore Scanner calls 3 called by 19

Entity Profile

Dependency Diagram

graph TD
  e5eb2faf_45a2_ac47_3404_8bd4e7eb6817["parse()"]
  77d1d37f_6021_dc8c_6d04_0c3b07e3ad19["migrateTheme()"]
  77d1d37f_6021_dc8c_6d04_0c3b07e3ad19 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  6dee7bcb_2b1c_cd79_688e_3f0f0ce8ea8f["migrateImport()"]
  6dee7bcb_2b1c_cd79_688e_3f0f0ce8ea8f -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  fe1ae328_7455_13dc_ecbe_a4e848ee9ea0["migratePreflight()"]
  fe1ae328_7455_13dc_ecbe_a4e848ee9ea0 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  f0fec366_dab0_6cd4_1266_ed1e249ce89a["substituteFunctionsInValue()"]
  f0fec366_dab0_6cd4_1266_ed1e249ce89a -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  529f2fd2_4c56_a018_3a52_f4d34e36e476["injectVar()"]
  529f2fd2_4c56_a018_3a52_f4d34e36e476 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  a5e6dda1_40ba_a775_d007_2d6576a30911["createConverter()"]
  a5e6dda1_40ba_a775_d007_2d6576a30911 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  324d33ab_fbf5_0679_98c5_2192fd427d97["substituteFunctionsInValue()"]
  324d33ab_fbf5_0679_98c5_2192fd427d97 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  258ace88_f7c6_412f_9b37_95e1bd249268["printArbitraryValueCache()"]
  258ace88_f7c6_412f_9b37_95e1bd249268 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  e3afcfef_5f31_4ec2_596b_857a628e6124["simplifyArbitraryVariantCache()"]
  e3afcfef_5f31_4ec2_596b_857a628e6124 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  6970a08e_50b2_87bb_2f15_453572b864d7["isVarCache()"]
  6970a08e_50b2_87bb_2f15_453572b864d7 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  3a1a48b0_f593_6ef7_a2a2_e2d97f76468f["constantFoldDeclaration()"]
  3a1a48b0_f593_6ef7_a2a2_e2d97f76468f -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  957946b3_94ca_8f71_30eb_76a6281c51df["theme()"]
  957946b3_94ca_8f71_30eb_76a6281c51df -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  5f56996c_e87d_a997_67df_2ad202639ad3["substituteFunctionsInValue()"]
  5f56996c_e87d_a997_67df_2ad202639ad3 -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  5af2f27c_4323_fb62_14cc_5b4f717b4d2f["createCssUtility()"]
  5af2f27c_4323_fb62_14cc_5b4f717b4d2f -->|calls| e5eb2faf_45a2_ac47_3404_8bd4e7eb6817
  style e5eb2faf_45a2_ac47_3404_8bd4e7eb6817 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/value-parser.ts lines 73–279

export function parse(input: string) {
  input = input.replaceAll('\r\n', '\n')

  let ast: ValueAstNode[] = []

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

  let parent = null as ValueFunctionNode | null

  let buffer = ''

  let peekChar

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

    switch (currentChar) {
      // Current character is a `\` therefore the next character is escaped,
      // consume it together with the next character and continue.
      case BACKSLASH: {
        buffer += input[i] + input[i + 1]
        i++
        break
      }

      // Typically for math operators, they have to have spaces around them. But
      // there are situations in `theme(colors.red.500/10)` where we use `/`
      // without spaces. Let's make sure this is a separate word as well.
      case SLASH: {
        // 1. Handle everything before the separator as a word
        // Handle everything before the closing paren as a word
        if (buffer.length > 0) {
          let node = word(buffer)
          if (parent) {
            parent.nodes.push(node)
          } else {
            ast.push(node)
          }
          buffer = ''
        }

        // 2. Track the `/` as a word on its own
        let node = word(input[i])
        if (parent) {
          parent.nodes.push(node)
        } else {
          ast.push(node)
        }

        break
      }

      // Space and commas are bundled into separators
      //
      // E.g.:
      //
      // ```css
      // foo(bar, baz)
      //        ^^
      // ```
      case COLON:
      case COMMA:
      case EQUALS:
      case GREATER_THAN:
      case LESS_THAN:
      case NEWLINE:
      case SPACE:
      case TAB: {
        // 1. Handle everything before the separator as a word
        // Handle everything before the closing paren as a word
        if (buffer.length > 0) {
          let node = word(buffer)
          if (parent) {
            parent.nodes.push(node)
          } else {
            ast.push(node)
          }
          buffer = ''
        }

        // 2. Look ahead and find the end of the separator
        let start = i
        let end = i + 1
        for (; end < input.length; end++) {
          peekChar = input.charCodeAt(end)
          if (
            peekChar !== COLON &&
            peekChar !== COMMA &&
            peekChar !== EQUALS &&
            peekChar !== GREATER_THAN &&
            peekChar !== LESS_THAN &&
            peekChar !== NEWLINE &&
            peekChar !== SPACE &&
            peekChar !== TAB
          ) {
            break
          }
        }
        i = end - 1

        let node = separator(input.slice(start, end))
        if (parent) {
          parent.nodes.push(node)
        } else {
          ast.push(node)
        }

        break
      }

      // Start of a string.
      case SINGLE_QUOTE:
      case DOUBLE_QUOTE: {
        let start = i

        // We need to ensure that the closing quote is the same as the opening
        // quote.
        //
        // E.g.:
        //
        // ```css
        // "This is a string with a 'quote' in it"
        //                          ^     ^         -> These are not the end of the string.
        // ```
        for (let j = i + 1; 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 string.
          else if (peekChar === currentChar) {
            i = j
            break
          }
        }

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

      // Start of a function call.
      //
      // E.g.:
      //
      // ```css
      // foo(bar, baz)
      //    ^
      // ```
      case OPEN_PAREN: {
        let node = fun(buffer, [])
        buffer = ''

        if (parent) {
          parent.nodes.push(node)
        } else {
          ast.push(node)
        }
        stack.push(node)
        parent = node

        break
      }

      // End of a function call.
      //
      // E.g.:
      //
      // ```css
      // foo(bar, baz)
      //             ^
      // ```
      case CLOSE_PAREN: {
        let tail = stack.pop()

        // Handle everything before the closing paren a word
        if (buffer.length > 0) {
          let node = word(buffer)
          tail?.nodes.push(node)
          buffer = ''
        }

        if (stack.length > 0) {
          parent = stack[stack.length - 1]
        } else {
          parent = null
        }

        break
      }

      // Everything else will be collected in the buffer
      default: {
        buffer += String.fromCharCode(currentChar)
      }
    }
  }

  // Collect the remainder as a word
  if (buffer.length > 0) {
    ast.push(word(buffer))
  }

  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 3 function(s): fun, separator, word.
What calls parse()?
parse() is called by 19 function(s): constantFoldDeclaration, createConverter, createConverterCache, createCssUtility, decodeArbitraryValue, extractUsedVariables, injectVar, isVarCache, and 11 more.

Analyze Your Own Codebase

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

Try Supermodel Free