erl_parse

Module

erl_parse

Module summary

The Erlang Parser

Description

This module is the basic Erlang parser which converts tokens into the abstract form of either forms (i.e., top-level constructs), expressions, or terms. The Abstract Format is described in the ERTS User's Guide. Note that a token list must end with the dot token in order to be acceptable to the parse functions (see erl_scan(3)).

Data types

abstract_clause() = term()

Parse tree for Erlang clause.

abstract_expr() = term()

Parse tree for Erlang expression.

abstract_form() = term()

Parse tree for Erlang form.

error_description() = term()

error_info() = {erl_anno:line(), module(), error_description()}

token() = erl_scan:token()

Exports

parse_form(Tokens) -> {ok, AbsForm} | {error, ErrorInfo}

Types:

Tokens = [token()]
AbsForm = abstract_form()
ErrorInfo = error_info()

This function parses Tokens as if it were a form. It returns:

{ok, AbsForm}

The parsing was successful. AbsForm is the abstract form of the parsed form.

{error, ErrorInfo}

An error occurred.

parse_exprs(Tokens) -> {ok, ExprList} | {error, ErrorInfo}

Types:

Tokens = [token()]
ExprList = [abstract_expr()]
ErrorInfo = error_info()

This function parses Tokens as if it were a list of expressions. It returns:

{ok, ExprList}

The parsing was successful. ExprList is a list of the abstract forms of the parsed expressions.

{error, ErrorInfo}

An error occurred.

parse_term(Tokens) -> {ok, Term} | {error, ErrorInfo}

Types:

Tokens = [token()]
Term = term()
ErrorInfo = error_info()

This function parses Tokens as if it were a term. It returns:

{ok, Term}

The parsing was successful. Term is the Erlang term corresponding to the token list.

{error, ErrorInfo}

An error occurred.

format_error(ErrorDescriptor) -> Chars

Types:

ErrorDescriptor = error_description()
Chars = [char() | Chars]

Uses an ErrorDescriptor and returns a string which describes the error. This function is usually called implicitly when an ErrorInfo structure is processed (see below).

tokens(AbsTerm) -> Tokenstokens(AbsTerm, MoreTokens) -> Tokens

Types:

AbsTerm = abstract_expr()
MoreTokens = Tokens = [token()]

This function generates a list of tokens representing the abstract form AbsTerm of an expression. Optionally, it appends MoreTokens.

normalise(AbsTerm) -> Data

Types:

AbsTerm = abstract_expr()
Data = term()

Converts the abstract form AbsTerm of a term into a conventional Erlang data structure (i.e., the term itself). This is the inverse of abstract/1.

abstract(Data) -> AbsTerm

Types:

Data = term()
AbsTerm = abstract_expr()

Converts the Erlang data structure Data into an abstract form of type AbsTerm. This is the inverse of normalise/1.

erl_parse:abstract(T) is equivalent to erl_parse:abstract(T, 0).

abstract(Data, Options) -> AbsTerm

Types:

Data = term()
Options = Line | [Option]
Option = {line, Line} | {encoding, Encoding}
Encoding = latin1 | unicode | utf8 | none | encoding_func()
Line = erl_anno:line()
AbsTerm = abstract_expr()
encoding_func() = fun((integer() >= 0) -> boolean())

Converts the Erlang data structure Data into an abstract form of type AbsTerm.

The Line option is the line that will be assigned to each node of the abstract form.

The Encoding option is used for selecting which integer lists will be considered as strings. The default is to use the encoding returned by epp:default_encoding/0. The value none means that no integer lists will be considered as strings. The encoding_func() will be called with one integer of a list at a time, and if it returns true for every integer the list will be considered a string.

map_anno(Fun, Abstr) -> NewAbstr

Types:

Fun = fun((Anno) -> Anno)
Anno = erl_anno:anno()
Abstr = NewAbstr = abstract_form() | abstract_expr()

Modifies the abstract form Abstr by applying Fun on every collection of annotations of the abstract form. The abstract form is traversed in a depth-first, left-to-right, fashion.

fold_anno(Fun, Acc0, Abstr) -> NewAbstr

Types:

Fun = fun((Anno, AccIn) -> AccOut)
Anno = erl_anno:anno()
Acc0 = AccIn = AccOut = term()
Abstr = NewAbstr = abstract_form() | abstract_expr()

Updates an accumulator by applying Fun on every collection of annotations of the abstract form Abstr. The first call to Fun has AccIn as argument, and the returned accumulator AccOut is passed to the next call, and so on. The final value of the accumulator is returned. The abstract form is traversed in a depth-first, left-to-right, fashion.

mapfold_anno(Fun, Acc0, Abstr) -> {NewAbstr, Acc1}

Types:

Fun = fun((Anno, AccIn) -> {Anno, AccOut})
Anno = erl_anno:anno()
Acc0 = Acc1 = AccIn = AccOut = term()
Abstr = NewAbstr = abstract_form() | abstract_expr()

Modifies the abstract form Abstr by applying Fun on every collection of annotations of the abstract form, while at the same time updating an accumulator. The first call to Fun has AccIn as second argument, and the returned accumulator AccOut is passed to the next call, and so on. The modified abstract form as well as the the final value of the accumulator is returned. The abstract form is traversed in a depth-first, left-to-right, fashion.

new_anno(Term) -> Abstr

Types:

Term = term()
Abstr = abstract_form() | abstract_expr()

Creates an abstract form from a term which has the same structure as an abstract form, but locations where the abstract form has annotations. For each location, erl_anno:new/1 is called, and the annotations replace the location.

anno_from_term(Term) -> abstract_form() | abstract_expr()

Types:

Term = term()

Assumes that Term is a term with the same structure as an abstract form, but with terms, T say, on those places where an abstract form has annotations. Returns an abstract form where every term T has been replaced by the value returned by calling erl_anno:from_term(T). The term Term is traversed in a depth-first, left-to-right, fashion.

anno_to_term(Abstr) -> term()

Types:

Abstr = abstract_form() | abstract_expr()

Returns a term where every collection of annotations Anno of Abstr has been replaced by the term returned by calling erl_anno:to_term(Anno). The abstract form is traversed in a depth-first, left-to-right, fashion.

Error Information

The ErrorInfo mentioned above is the standard ErrorInfo structure which is returned from all IO modules. It has the format:

{ErrorLine, Module, ErrorDescriptor}    

A string which describes the error is obtained with the following call:

Module:format_error(ErrorDescriptor)    

See Also

io(3), erl_anno(3), erl_scan(3), ERTS User's Guide

© 2010–2017 Ericsson AB
Licensed under the Apache License, Version 2.0.