erl_scan(3erl)
π
erl_scan β the Erlang token scanner, but make it make sense β¨
okay so bestie, this is the erl_scan module from the Erlang standard library docs (stdlib), and it's literally the module that turns your raw characters into Erlang tokens. no cap, it's the main character of the whole lexing situation. let's get into it π«Ά
πΈ NAME πΈ
erl_scan β The Erlang token scanner. that's it, that's the name, iconic.
π¦ SYNOPSIS π¦
this page is about the module, so no synopsis line here β just know that everything below is exported by erl_scan and you call it from your Erlang code like a normal person π
β¨ DESCRIPTION β¨
This module contains functions for tokenizing (scanning) characters into Erlang tokens. so yeah β you feed it characters, it hands you back tokens. literal glow-up for your strings π
π DATA TYPES π
girl, this is the type-alias section, and it's giving very "here's the vocabulary before the vibes":
category() = atom()
error_description() = term()
error_info() =
{erl_anno:location(), module(), error_description()}
option() =
return | return_white_spaces | return_comments | text |
{reserved_word_fun, resword_fun()} |
{text_fun, text_fun()}
options() = option() | [option()]
symbol() = atom() | float() | integer() | string()
resword_fun() = fun((atom()) -> boolean())
token() =
{category(), Anno :: erl_anno:anno(), symbol()} |
{category(), Anno :: erl_anno:anno()}
tokens() = [token()]
tokens_result() =
{ok, Tokens :: tokens(), EndLocation :: erl_anno:location()} |
{eof, EndLocation :: erl_anno:location()} |
{error,
ErrorInfo :: error_info(),
EndLocation :: erl_anno:location()}
text_fun() = fun((atom(), string()) -> boolean())
copy these exactly, bestie, they're load-bearing π
π₯ EXPORTS π₯
category(Token) -> category()
Types:
Token = token()
Returns the category of Token. one job, does it slay.
column(Token) -> erl_anno:column() | undefined
Types:
Token = token()
Returns the column of Token's collection of annotations. cute little column check π
end_location(Token) -> erl_anno:location() | undefined
Types:
Token = token()
Returns the end location of the text of Token's collection of annotations. If there is no text, undefined is returned. no text? no location. periodt.
format_error(ErrorDescriptor) -> string()
Types:
ErrorDescriptor = error_description()
Uses an ErrorDescriptor and returns a string that describes the error or warning. This function is usually called implicitly when an ErrorInfo structure is processed (see section Error Information). so like, you don't usually call it yourself, it just happens π§
line(Token) -> erl_anno:line()
Types:
Token = token()
Returns the line of Token's collection of annotations. line go brrr π
location(Token) -> erl_anno:location()
Types:
Token = token()
Returns the location of Token's collection of annotations. the where-is-it of it all π
reserved_word(Atom :: atom()) -> boolean()
Returns true if Atom is an Erlang reserved word, otherwise false. simple, clean, no notes π
string(String) -> Return
string(String, StartLocation) -> Return
string(String, StartLocation, Options) -> Return
Types:
String = string()
Options = options()
Return =
{ok, Tokens :: tokens(), EndLocation} |
{error, ErrorInfo :: error_info(), ErrorLocation}
StartLocation = EndLocation = ErrorLocation =
erl_anno:location()
Takes the list of characters String and tries to scan (tokenize) them. Returns one of the following:
{ok, Tokens, EndLocation}:Tokensare the Erlang tokens fromString.EndLocationis the first location after the last token. slay, you got tokens β¨{error, ErrorInfo, ErrorLocation}: An error occurred.ErrorLocationis the first location after the erroneous token. π rude but informative.
string(String) is equivalent to string(String, 1), and string(String, StartLocation) is equivalent to string(String, StartLocation, []). free shortcuts, we love to see it.
StartLocation indicates the initial location when scanning starts. If StartLocation is a line, Anno, EndLocation, and ErrorLocation are lines. If StartLocation is a pair of a line and a column, Anno takes the form of an opaque compound data type, and EndLocation and ErrorLocation are pairs of a line and a column. The token annotations contain information about the column and the line where the token begins, as well as the text of the token (if option text is specified), all of which can be accessed by calling column/1, line/1, location/1, and text/1. okay so annotations are the receipts of where everything came from, got it π§Ύ
A token is a tuple containing information about syntactic category, the token annotations, and the terminal symbol. For punctuation characters (such as ; and |) and reserved words, the category and the symbol coincide, and the token is represented by a two-tuple. Three-tuples have one of the following forms:
{atom, Anno, atom()}{char, Anno, char()}{comment, Anno, string()}{float, Anno, float()}{integer, Anno, integer()}{var, Anno, atom()}{white_space, Anno, string()}
Valid options β okay bestie, this is the options menu, read it all:
{reserved_word_fun, reserved_word_fun()}: A callback function that is called when the scanner has found an unquoted atom. If the function returnstrue, the unquoted atom itself becomes the category of the token. If the function returnsfalse,atombecomes the category of the unquoted atom. you decide the fate of the atom πreturn_comments: Return comment tokens. give me my comments back πreturn_white_spaces: Return white space tokens. By convention, a newline character, if present, is always the first character of the text (there cannot be more than one newline in a white space token). whitespace girlies rise πΈreturn: Short for[return_comments, return_white_spaces]. the combo meal deal.text: Include the token text in the token annotation. The text is the part of the input corresponding to the token. See alsotext_fun. keep the receipts π{text_fun, text_fun()}: A callback function used to determine whether the full text for the token shall be included in the token annotation. Arguments of the function are the category of the token and the full token string. This is only used whentextis not present. If neither are present the text will not be saved in the token annotation. so:textwins, thentext_fun, then nothing. iykyk.
symbol(Token) -> symbol()
Types:
Token = token()
Returns the symbol of Token. the actual meat of the token π
text(Token) -> erl_anno:text() | undefined
Types:
Token = token()
Returns the text of Token's collection of annotations. If there is no text, undefined is returned. same energy as end_location, no text no problem π
tokens(Continuation, CharSpec, StartLocation) -> Return
tokens(Continuation, CharSpec, StartLocation, Options) -> Return
Types:
Continuation = return_cont() | []
CharSpec = char_spec()
StartLocation = erl_anno:location()
Options = options()
Return =
{done,
Result :: tokens_result(),
LeftOverChars :: char_spec()} |
{more, Continuation1 :: return_cont()}
char_spec() = string() | eof
return_cont()
An opaque continuation.
This is the re-entrant scanner, which scans characters until either a dot ('.' followed by a white space) or eof is reached. It returns:
{done, Result, LeftOverChars}: Indicates that there is sufficient input data to get a result.Resultis:{ok, Tokens, EndLocation}: The scanning was successful.Tokensis the list of tokens including dot. π{eof, EndLocation}: End of file was encountered before any more tokens. that's all folks.{error, ErrorInfo, EndLocation}: An error occurred.LeftOverCharsis the remaining characters of the input data, starting fromEndLocation. leftovers, but make it lexing π
{more, Continuation1}: More data is required for building a term.Continuation1must be passed in a new call totokens/3,4when more data is available. come back when you have more, bestie.
The CharSpec eof signals end of file. LeftOverChars then takes the value eof as well.
tokens(Continuation, CharSpec, StartLocation) is equivalent to tokens(Continuation, CharSpec, StartLocation, []). yes girl, the shortcut again.
For a description of the options, see string/3. go back up, it's all there π
π§ ERROR INFORMATION π§
ErrorInfo is the standard ErrorInfo structure that is returned from all I/O modules. The format is as follows:
{ErrorLocation, Module, ErrorDescriptor}
A string describing the error is obtained with the following call:
Module:format_error(ErrorDescriptor)
that's the whole error-handling vibe, no notes π
β NOTES β
The continuation of the first call to the re-entrant input functions must be []. like, literally must be. For a complete description of how the re-entrant input scheme works, see Armstrong, Virding and Williams: 'Concurrent Programming in Erlang', Chapter 13. homework assignment, but make it iconic π
bestie, real talk: that [] on the first call isn't optional, it's the entry fee π
π¦ SEE ALSO π¦
erl_anno(3erl), erl_parse(3erl), io(3erl)
go read those too, they're the supporting cast and they ate π
and that's the whole page, bestie β written by Ericsson AB, stdlib version 4.3.1.3, dated alongside this erl_scan(3erl) manual page. mwah πβ¨π
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms