Calculemus


Lexer Module

Lexical analysis.

Note

The functions for lexical analysis in the original code where in the Intro module and have been moved here for isolation purposes.

Functions and values

Function or value Description

alphanumeric s

Full Usage: alphanumeric s

Parameters:
    s : string - The single character string to be classified.

Returns: bool true if the single character string it is considered alphanumeric, otherwise false.

Classifies single character strings as alphanumeric.

s : string

The single character string to be classified.

Returns: bool

true if the single character string it is considered alphanumeric, otherwise false.

Example

 alphanumeric "1"
Evaluates to true.

Example

 alphanumeric "z"
Evaluates to true.

Example

 alphanumeric "."
Evaluates to false.

lex inp

Full Usage: lex inp

Parameters:
    inp : string list - The input list of single character strings to be tokenized.

Returns: string list The input list of single character strings tokenized.

Lexical analyser.

It maps a list of input characters inp into a list of token strings.

inp : string list

The input list of single character strings to be tokenized.

Returns: string list

The input list of single character strings tokenized.

Example

 "((11 + 2) * x_1)"
 |> explode
 |> lex
Evaluates to ["("; "("; "11"; "+"; "2"; ")"; "*"; "x_1"; ")"]. Note how 11 and x_1 are analyzed as a single tokens.

lexwhile prop inp

Full Usage: lexwhile prop inp

Parameters:
    prop : string -> bool - The predicate to identify tokens.
    inp : string list - The input list of single character strings.

Returns: string * string list A pair with the longest initial sequence of elements of inp classifiable as satisfying prop as the first component, and the remaining characters as the second.

Takes a property prop of characters (such as one of the classifying predicates: Lexer.space, Lexer.punctuation, Lexer.symbolic, Lexer.numeric, Lexer.alphanumeric) and a list of single character strings inp, separating off as a string the longest initial sequence of that list of characters satisfying prop.

prop : string -> bool

The predicate to identify tokens.

inp : string list

The input list of single character strings.

Returns: string * string list

A pair with the longest initial sequence of elements of inp classifiable as satisfying prop as the first component, and the remaining characters as the second.

Example

 "((1 + 2) * x_1)"
 |> explode
 |> lexwhile punctuation
Evaluates to ("((", ["1"; " "; "+"; " "; "2"; ")"; " "; "*"; " "; "x"; "_"; "1"; ")"]).

matches s

Full Usage: matches s

Parameters:
    s : string - The string of all characters to be matched.

Returns: string -> bool A function that applied to a single character string checks if it matches the given pattern.

Creates a pattern matching function based on the input string s as the pattern.

s : string

The string of all characters to be matched.

Returns: string -> bool

A function that applied to a single character string checks if it matches the given pattern.

Example

 matches "abc" "a"
Evaluates to true.

Example

 matches "abc" "d"
Evaluates to false.

numeric s

Full Usage: numeric s

Parameters:
    s : string - The single character string to be classified.

Returns: bool true if the single character string it is considered numeric, otherwise false.

Classifies single character strings as numeric.

s : string

The single character string to be classified.

Returns: bool

true if the single character string it is considered numeric, otherwise false.

Example

 numeric "1"
Evaluates to true.

Example

 numeric "z"
Evaluates to false.

punctuation s

Full Usage: punctuation s

Parameters:
    s : string - The single character string to be classified.

Returns: bool true if the single character string it is considered a punctuation symbol, otherwise false.

Classifies single character strings as punctuation symbols: ()[]{},.

s : string

The single character string to be classified.

Returns: bool

true if the single character string it is considered a punctuation symbol, otherwise false.

Example

 punctuation ","
Evaluates to true.

Example

 punctuation "."
Evaluates to false.

space s

Full Usage: space s

Parameters:
    s : string - The single character string to be classified.

Returns: bool true if the single character string it is considered a space, otherwise false.

Classifies single character strings as spaces.

Tabs and new lines are also considered spaces.

s : string

The single character string to be classified.

Returns: bool

true if the single character string it is considered a space, otherwise false.

Example

 space " "
Evaluates to true.

Example

 space "."
Evaluates to false.

symbolic s

Full Usage: symbolic s

Parameters:
    s : string - The single character string to be classified.

Returns: bool true if the single character string it is considered symbolic, otherwise false.

Classifies single character strings as symbolic: ~`!@#$%^&*-+=|\\:;<>.?/.

s : string

The single character string to be classified.

Returns: bool

true if the single character string it is considered symbolic, otherwise false.

Example

 symbolic "."
Evaluates to true.

Example

 symbolic "1"
Evaluates to false.