export function fluentParsePattern()

in js/src/fluent-parse.ts [31:77]


export function fluentParsePattern(
  src: string,
  onError: (error: ParseError) => void
): Pattern {
  const pattern: Pattern = []
  const ps = new FluentParserStream(src)
  let ch
  try {
    while ((ch = ps.currentChar())) {
      switch (ch) {
        case '{': {
          ps.next()
          ps.skipBlank()
          const expr = expression(ps)
          ps.skipBlank()
          if (ps.currentChar() === '-' && ps.peek() === '>') {
            // Not supporting selectors within patterns
            throw new FluentError('E0028')
          }
          ps.expectChar('}')
          pattern.push(expr)
          break
        }
        case '}':
          throw new FluentError('E0027')
        default: {
          let buffer = ''
          let ch
          while ((ch = ps.currentChar())) {
            if (ch === '{' || ch === '}') break
            buffer += ch
            ps.next()
          }
          pattern.push(buffer)
        }
      }
    }
  } catch (error) {
    const msg =
      error instanceof FluentError
        ? `fluent: ${error.message} (${error.code})`
        : `fluent: ${error}`
    onError(new ParseError(msg, ps.index, src.length))
    if (ps.index < src.length) pattern.push(src.substring(ps.index))
  }
  return pattern
}