檢視原始碼 erl_pp (stdlib v6.2)
Erlang 美化列印器。
此模組中的函式用於產生抽象形式的美觀表示,適用於列印。所有函式都會傳回(可能很深的)字元列表,如果形式錯誤則會產生錯誤。
所有函式都可以有一個可選引數,該引數指定一個鉤子(hook),如果在嘗試列印未知形式時會呼叫該鉤子。
請注意,如果此模組中的函式用於將抽象程式碼轉換回 Erlang 原始碼,則封閉函式應先由 legalize_vars/1
處理,以確保輸出在語義上與抽象程式碼等效。
已知限制
除了表達式之外,無法在其他地方為未知形式設定鉤子函式。
另請參閱
摘要
類型
此模組中描述的函式中顯示的可選引數 HookFunction
定義了一個函式,當在應為有效表達式的地方出現未知形式時會呼叫該函式。如果 HookFunction
等於 none
,則沒有鉤子函式。
選項 quote_singleton_atom_types
用於將引號新增到所有單例原子類型。
函式
與 form/1,2
相同,但僅適用於屬性 Attribute
。
與 form/1,2
相同,但僅適用於 Expressions
中的表達式序列。
美化列印 Form
,這是 erl_parse:parse_form/1
傳回的一種抽象形式。
與 form/1,2
相同,但僅適用於函式 Function
。
與 form/1,2
相同,但僅適用於保護測試 Guard
。
當將記錄展開為元組時,Erlang 編譯器會在抽象表示中引入新的變數。由於展開是在抽象表示上完成的,因此編譯器可以安全地使用在 Erlang 原始程式碼中語法上無效的名稱來命名新的變數(名稱以小寫字母開頭),從而確保新名稱的唯一性。
類型
-type hook_function() :: none | fun((Expr :: erl_parse:abstract_expr(), CurrentIndentation :: integer(), CurrentPrecedence :: non_neg_integer(), Options :: options()) -> io_lib:chars()).
此模組中描述的函式中顯示的可選引數 HookFunction
定義了一個函式,當在應為有效表達式的地方出現未知形式時會呼叫該函式。如果 HookFunction
等於 none
,則沒有鉤子函式。
被呼叫的鉤子函式應傳回(可能很深的)字元列表。函式 expr/4
在鉤子中很有用。
如果 CurrentIndentation
為負數,則沒有換行符,僅使用空格作為分隔符。
-type option() :: {hook, hook_function()} | {encoding, latin1 | unicode | utf8} | {quote_singleton_atom_types, boolean()} | {linewidth, pos_integer()} | {indent, pos_integer()}.
選項 quote_singleton_atom_types
用於將引號新增到所有單例原子類型。
選項 linewidth
控制格式化行的最大行寬(預設為 72 個字元)。
選項 indent
控制格式化行的縮排(預設為 4 個空格)。
-type options() :: hook_function() | [option()].
函式
-spec attribute(Attribute) -> io_lib:chars() when Attribute :: erl_parse:abstract_form().
-spec attribute(Attribute, Options) -> io_lib:chars() when Attribute :: erl_parse:abstract_form(), Options :: options().
與 form/1,2
相同,但僅適用於屬性 Attribute
。
-spec expr(Expression) -> io_lib:chars() when Expression :: erl_parse:abstract_expr().
-spec expr(Expression, Options) -> io_lib:chars() when Expression :: erl_parse:abstract_expr(), Options :: options().
-spec expr(Expression, Indent, Options) -> io_lib:chars() when Expression :: erl_parse:abstract_expr(), Indent :: integer(), Options :: options().
-spec expr(Expression, Indent, Precedence, Options) -> io_lib:chars() when Expression :: erl_parse:abstract_expr(), Indent :: integer(), Precedence :: non_neg_integer(), Options :: options().
列印一個表達式。
它對於實現鉤子很有用(請參閱 已知限制 一節)。
-spec exprs(Expressions) -> io_lib:chars() when Expressions :: [erl_parse:abstract_expr()].
-spec exprs(Expressions, Options) -> io_lib:chars() when Expressions :: [erl_parse:abstract_expr()], Options :: options().
-spec exprs(Expressions, Indent, Options) -> io_lib:chars() when Expressions :: [erl_parse:abstract_expr()], Indent :: integer(), Options :: options().
與 form/1,2
相同,但僅適用於 Expressions
中的表達式序列。
-spec form(Form) -> io_lib:chars() when Form :: erl_parse:abstract_form() | erl_parse:form_info().
等效於 form(Form, none)
。
-spec form(Form, Options) -> io_lib:chars() when Form :: erl_parse:abstract_form() | erl_parse:form_info(), Options :: options().
美化列印 Form
,這是 erl_parse:parse_form/1
傳回的一種抽象形式。
-spec function(Function) -> io_lib:chars() when Function :: erl_parse:abstract_form().
-spec function(Function, Options) -> io_lib:chars() when Function :: erl_parse:abstract_form(), Options :: options().
與 form/1,2
相同,但僅適用於函式 Function
。
-spec guard(Guard) -> io_lib:chars() when Guard :: [erl_parse:abstract_expr()].
等效於 guard(Guard, none)
。
-spec guard(Guard, Options) -> io_lib:chars() when Guard :: [erl_parse:abstract_expr()], Options :: options().
與 form/1,2
相同,但僅適用於保護測試 Guard
。
-spec legalize_vars(Function) -> erl_parse:abstract_form() when Function :: erl_parse:abstract_form().
當將記錄展開為元組時,Erlang 編譯器會在抽象表示中引入新的變數。由於展開是在抽象表示上完成的,因此編譯器可以安全地使用在 Erlang 原始程式碼中語法上無效的名稱來命名新的變數(名稱以小寫字母開頭),從而確保新名稱的唯一性。
如果使用者想要使用此模組的函式將抽象表示轉換回 Erlang 原始程式碼,則上述策略會導致問題。通常,模式變數會輸出為原子,從而更改程式的語義。為了解決這個問題,legalize_vars/1
在函式的抽象表示上執行時,將傳回一個等效的函式,其中所有變數都具有語法上有效的名稱。