pearl

Types

pub type Error {
  UnknownCharacter(character: String)
  UnterminatedStringLiteral
  UnterminatedQuotedAtom
  InvalidRadix(radix: String)
  NumericSeparatorNotAllowed
  ExpectedExponent
  NumberCannotEndAfterRadix
  UnterminatedCharacter
  UnterminatedEscapeSequence
  ExpectedSigilDelimiter
  ExpectedWhitespaceAfterTripleQuote
  InvalidTripleQuotedStringIndentation(
    expected_indentation: String,
    line: String,
  )
}

Constructors

  • UnknownCharacter(character: String)
  • UnterminatedStringLiteral
  • UnterminatedQuotedAtom
  • InvalidRadix(radix: String)
  • NumericSeparatorNotAllowed
  • ExpectedExponent
  • NumberCannotEndAfterRadix
  • UnterminatedCharacter
  • UnterminatedEscapeSequence
  • ExpectedSigilDelimiter
  • ExpectedWhitespaceAfterTripleQuote
  • InvalidTripleQuotedStringIndentation(
      expected_indentation: String,
      line: String,
    )

A highlighting token, containing information about the kind of syntax being used. Many similar tokens (e.g. all keywords) are grouped together to simplify them.

For syntax tokens, see Token.

pub type HighlightToken {
  HighlightWhitespace(String)
  HighlightKeyword(String)
  HighlightVariable(String)
  HighlightString(String)
  HighlightAtom(String)
  HighlightNumber(String)
  HighlightModule(String)
  HighlightFunction(String)
  HighlightOperator(String)
  HighlightComment(String)
  HighlightPunctuation(String)
  HighlightOther(String)
}

Constructors

  • HighlightWhitespace(String)
  • HighlightKeyword(String)
  • HighlightVariable(String)
  • HighlightString(String)
  • HighlightAtom(String)
  • HighlightNumber(String)
  • HighlightModule(String)
  • HighlightFunction(String)
  • HighlightOperator(String)
  • HighlightComment(String)
  • HighlightPunctuation(String)
  • HighlightOther(String)
pub opaque type Lexer
pub type SigilDelimiter {
  SigilNone
  SigilParen
  SigilSquare
  SigilBrace
  SigilAngle
  SigilSlash
  SigilPipe
  SigilSingleQuote
  SigilDoubleQuote
  SigilBacktick
  SigilHash
}

Constructors

  • SigilNone
  • SigilParen
  • SigilSquare
  • SigilBrace
  • SigilAngle
  • SigilSlash
  • SigilPipe
  • SigilSingleQuote
  • SigilDoubleQuote
  • SigilBacktick
  • SigilHash
pub type Token {
  Whitespace(String)
  Comment(String)
  DocComment(String)
  ModuleComment(String)
  EndOfFile
  Character(String)
  Integer(String)
  Float(String)
  Atom(name: String, quoted: Bool)
  String(String)
  TripleQuotedString(
    sigil: option.Option(String),
    number_of_quotes: Int,
    beginning_whitespace: String,
    lines: List(String),
    end_indentation: String,
  )
  Sigil(
    sigil: String,
    delimiter: SigilDelimiter,
    contents: String,
  )
  Variable(String)
  After
  Begin
  Case
  Catch
  Cond
  Else
  End
  Fun
  If
  Let
  Maybe
  Of
  Receive
  Try
  When
  LeftParen
  RightParen
  LeftBrace
  RightBrace
  LeftSquare
  RightSquare
  Comma
  Semicolon
  Colon
  Dot
  MinusGreater
  DoubleLess
  DoubleGreater
  Hash
  DoubleColon
  DoubleDot
  TripleDot
  DoublePipe
  EqualGreater
  ColonEqual
  LessMinus
  LessEqual
  Pipe
  DoubleEqual
  SlashEqual
  EqualLess
  Less
  GreaterEqual
  Greater
  EqualColonEqual
  EqualSlashEqual
  Plus
  Minus
  Star
  Slash
  Bnot
  Div
  Rem
  Band
  Bor
  Bxor
  Bsl
  Bsr
  Not
  And
  Or
  Xor
  Andalso
  Orelse
  DoublePlus
  DoubleMinus
  QuestionEqual
  Question
  Bang
  Equal
  Unknown(String)
  UnterminatedString(String)
  UnterminatedSigil(
    sigil: String,
    delimiter: SigilDelimiter,
    contents: String,
  )
  UnterminatedAtom(String)
  InvalidTripleQuotedString(contents: String)
}

Constructors

  • Whitespace(String)
  • Comment(String)
  • DocComment(String)
  • ModuleComment(String)
  • EndOfFile
  • Character(String)
  • Integer(String)
  • Float(String)
  • Atom(name: String, quoted: Bool)
  • String(String)
  • TripleQuotedString(
      sigil: option.Option(String),
      number_of_quotes: Int,
      beginning_whitespace: String,
      lines: List(String),
      end_indentation: String,
    )
  • Sigil(sigil: String, delimiter: SigilDelimiter, contents: String)
  • Variable(String)
  • After
  • Begin
  • Case
  • Catch
  • Cond
  • Else
  • End
  • Fun
  • If
  • Let
  • Maybe
  • Of
  • Receive
  • Try
  • When
  • LeftParen
  • RightParen
  • LeftBrace
  • RightBrace
  • LeftSquare
  • RightSquare
  • Comma
  • Semicolon
  • Colon
  • Dot
  • MinusGreater
  • DoubleLess
  • DoubleGreater
  • Hash
  • DoubleColon
  • DoubleDot
  • TripleDot
  • DoublePipe
  • EqualGreater
  • ColonEqual
  • LessMinus
  • LessEqual
  • Pipe
  • DoubleEqual
  • SlashEqual
  • EqualLess
  • Less
  • GreaterEqual
  • Greater
  • EqualColonEqual
  • EqualSlashEqual
  • Plus
  • Minus
  • Star
  • Slash
  • Bnot
  • Div
  • Rem
  • Band
  • Bor
  • Bxor
  • Bsl
  • Bsr
  • Not
  • And
  • Or
  • Xor
  • Andalso
  • Orelse
  • DoublePlus
  • DoubleMinus
  • QuestionEqual
  • Question
  • Bang
  • Equal
  • Unknown(String)
  • UnterminatedString(String)
  • UnterminatedSigil(
      sigil: String,
      delimiter: SigilDelimiter,
      contents: String,
    )
  • UnterminatedAtom(String)
  • InvalidTripleQuotedString(contents: String)

Values

pub fn highlight_ansi(code: String) -> String

Convert a string of Erlang source code into ansi highlighting.

Colours taken from contour:

TokenColour
KeywordYellow
ModuleCyan
FunctionBlue
OperatorMagenta
CommentItalic grey
String, Number, AtomGreen
Whitespace, VariableNo colour

If you wish to use other colours or another format, use to_tokens.

pub fn highlight_html(code: String) -> String

Convert a string of Erlang source code into an HTML string. Each token is wrapped in a <span> with a class indicating the type of

Class names taken from contour:

TokenCSS class
Keywordhl-keyword
Variablehl-variable
Modulehl-module
Functionhl-function
Operatorhl-operator
Punctuationhl-punctuation
Commenthl-comment
Stringhl-string
Atomhl-atom
Numberhl-number
Whitespaceno class

Place the output within a <pre><code>...</code></pre> and add styling for these CSS classes to get highlighting on your website. Here’s some CSS you could use:

pre code .hl-comment  { color: #d4d4d4; font-style: italic }
pre code .hl-function { color: #9ce7ff }
pre code .hl-keyword  { color: #ffd596 }
pre code .hl-operator { color: #ffaff3 }
pre code .hl-string   { color: #c8ffa7 }
pre code .hl-number   { color: #c8ffa7 }
pre code .hl-atom     { color: #c8ffa7 }
pre code .hl-module   { color: #ffddfa }

If you wish to use another format see to_ansi or to_tokens.

pub fn highlight_tokens(code: String) -> List(HighlightToken)

Convert a string of Erlang source code into highlighting tokens. Highlighting tokens only contain information about the kind of syntax being used, grouping similar tokens (e.g. all keywords) into one category.

To convert code into syntax tokens, see pearl.tokenise.

pub fn ignore_comments(lexer: Lexer) -> Lexer
pub fn ignore_whitespace(lexer: Lexer) -> Lexer
pub fn new(source: String) -> Lexer
pub fn sigil_delimiters(
  delimiter: SigilDelimiter,
) -> #(String, String)

Get the beginning and ending characters for a sigil

pub fn stringify_error(error: Error) -> String
pub fn to_source(tokens: List(Token)) -> String

Convert a list of tokens back to their original source code

pub fn token_to_source(token: Token) -> String

Convert a token back to its source code representation

pub fn tokenise(lexer: Lexer) -> #(List(Token), List(Error))
Search Document