arrow-parser 0.0.2

Parser for the Arrow programming language
Documentation
use super::error::SyntaxError;
use super::error::SyntaxErrorType;
use super::source::SourceRange;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[repr(u8)]
pub enum TokenType {
  // Special token used to represent the end of the source code. Easier than using and handling Option everywhere.
  EOF,

  // Only used during lexing.
  _LiteralNumberBin,
  _LiteralNumberDec,
  _LiteralNumberHex,
  _LiteralNumberOct,

  Ampersand,
  AmpersandAmpersand,
  AmpersandAmpersandEquals,
  AmpersandEquals,
  Asterisk,
  AsteriskAsterisk,
  AsteriskAsteriskEquals,
  AsteriskEquals,
  Bar,
  BarBar,
  BarBarEquals,
  BarEquals,
  BraceClose,
  BraceOpen,
  BracketClose,
  BracketOpen,
  Caret,
  CaretEquals,
  ChevronLeft,
  ChevronLeftChevronLeft,
  ChevronLeftChevronLeftEquals,
  ChevronLeftEquals,
  ChevronLeftSlash,
  ChevronRight,
  ChevronRightChevronRight,
  ChevronRightChevronRightChevronRight,
  ChevronRightChevronRightChevronRightEquals,
  ChevronRightChevronRightEquals,
  ChevronRightEquals,
  Colon,
  ColonColon,
  Comma,
  CommentMultiple,
  CommentSingle,
  Dot,
  DotDotDot,
  Equals,
  EqualsEquals,
  Exclamation,
  ExclamationEquals,
  Hyphen,
  HyphenChevronRight,
  HyphenEquals,
  Identifier,
  KeywordAs,
  KeywordBreak,
  KeywordContinue,
  KeywordCrate,
  KeywordElse,
  KeywordFor,
  KeywordIf,
  KeywordImpl,
  KeywordIn,
  KeywordLet,
  KeywordLoop,
  KeywordMut,
  KeywordPub,
  KeywordReturn,
  KeywordStruct,
  LiteralBigInt,
  LiteralDecimal,
  LiteralFalse,
  LiteralFloat,
  LiteralInt,
  LiteralNone,
  LiteralTemplatePartString,
  LiteralTemplatePartStringEnd,
  LiteralTrue,
  ParenthesisClose,
  ParenthesisOpen,
  Percent,
  PercentEquals,
  Plus,
  PlusEquals,
  QuestionBracketOpen,
  QuestionDot,
  QuestionParenthesisOpen,
  QuestionQuestion,
  QuestionQuestionEquals,
  Semicolon,
  Slash,
  SlashEquals,
}

#[derive(Clone, Copy)]
pub struct TokenTypeSet(u128);

impl TokenTypeSet {
  pub fn new(vals: &[TokenType]) -> TokenTypeSet {
    let mut set = 0u128;
    for t in vals.iter().cloned() {
      set |= 1u128 << (t as u8);
    }
    TokenTypeSet(set)
  }

  pub fn contains(self, t: TokenType) -> bool {
    (self.0 & (1u128 << (t as u8))) != 0
  }
}

#[derive(Clone)]
pub struct Token {
  pub loc: SourceRange,
  pub typ: TokenType,
}

impl Token {
  pub fn new(loc: SourceRange, typ: TokenType) -> Token {
    Token { loc, typ }
  }

  pub fn error(&self, typ: SyntaxErrorType) -> SyntaxError {
    SyntaxError::from_loc(self.loc, typ, Some(self.typ.clone()))
  }
}