///|
/// Interpolation piece inside a string or bytes literal.
#derive(Eq, Debug)
pub(all) enum RawStringPart {
  Text(String)
  ExprSource(String, Span)
}

///|
/// All lexical tokens in the CUE language specification (Core).
#derive(Eq, Debug)
pub(all) enum TokenKind {
  // End of file
  Eof

  // Literals
  Ident(String) // foo, #Def, _hidden, _#hiddenDef
  IntLit(Int64, String) // parsed value, raw representation
  FloatLit(Double, String) // parsed value, raw representation
  StringLit(String) // plain string literal
  BytesLit(Bytes) // plain bytes literal
  InterpString(Array[RawStringPart]) // string containing \(...) interpolations
  InterpBytes(Array[RawStringPart]) // bytes containing \(...) interpolations

  // Keywords
  Package
  Import
  For
  In
  If
  Let
  Null
  True
  False

  // Special CUE tokens
  Bottom // _|_
  Ellipsis // ...

  // Operators
  Add // +
  Sub // -
  Mul // *
  Quo // /
  And // &
  Or // |
  Land // &&
  Lor // ||
  Eq // ==
  Neq // !=
  Lt // <
  Lte // <=
  Gt // >
  Gte // >=
  Match // =~
  NotMatch // !~
  Not // !
  Bind // =
  Assign // :=
  Arrow // <-

  // Delimiters & punctuation
  Colon // :
  Comma // ,
  Dot // .
  Question // ?
  LParen // (
  RParen // )
  LBrace // {
  RBrace // }
  LBrack // [
  RBrack // ]
}

///|
pub impl Show for TokenKind with fn output(self, logger) {
  let s = match self {
    Eof => "EOF"
    Ident(n) => "ident(\{n})"
    IntLit(_, raw) => "int(\{raw})"
    FloatLit(_, raw) => "float(\{raw})"
    StringLit(v) => "\"\{v}\""
    BytesLit(_) => "bytes"
    InterpString(_) => "interpolated string"
    InterpBytes(_) => "interpolated bytes"
    Package => "'package'"
    Import => "'import'"
    For => "'for'"
    In => "'in'"
    If => "'if'"
    Let => "'let'"
    Null => "'null'"
    True => "'true'"
    False => "'false'"
    Bottom => "'_|_'"
    Ellipsis => "'...'"
    Add => "'+'"
    Sub => "'-'"
    Mul => "'*'"
    Quo => "'/'"
    And => "'&'"
    Or => "'|'"
    Land => "'&&'"
    Lor => "'||'"
    Eq => "'=='"
    Neq => "'!='"
    Lt => "'<'"
    Lte => "'<='"
    Gt => "'>'"
    Gte => "'>='"
    Match => "'=~'"
    NotMatch => "'!~'"
    Not => "'!'"
    Bind => "'='"
    Assign => "':='"
    Arrow => "'<-'"
    Colon => "':'"
    Comma => "','"
    Dot => "'.'"
    Question => "'?'"
    LParen => "'('"
    RParen => "')'"
    LBrace => "'{'"
    RBrace => "'}'"
    LBrack => "'['"
    RBrack => "']'"
  }
  logger.write_string(s)
}

///|
/// Token with its source span.
#derive(Eq, Debug)
pub(all) struct Token {
  kind : TokenKind
  span : Span
}

///|
pub fn Token::new(kind : TokenKind, span : Span) -> Token {
  { kind, span, }
}

///|
/// Returns true if a newline following this token triggers automatic comma insertion
/// per the CUE Language Specification.
pub fn TokenKind::triggers_auto_comma(self : TokenKind) -> Bool {
  match self {
    Ident(_)
    | IntLit(_, _)
    | FloatLit(_, _)
    | StringLit(_)
    | BytesLit(_)
    | InterpString(_)
    | InterpBytes(_)
    | Null
    | True
    | False
    | Bottom
    | Ellipsis
    | Question
    | Not
    | RParen
    | RBrack
    | RBrace => true
    _ => false
  }
}