檢視原始碼 epp (stdlib v6.2)
Erlang 程式碼預處理器。
Erlang 程式碼預處理器包含 compile
模組所使用的函式,用於在剖析之前預處理巨集和 include 檔案。
Erlang 原始檔的 編碼 是由原始檔前兩行中的註解選取的。第一個符合正規表示式 coding\s*[:=]\s*([-a-zA-Z0-9])+
的字串會選取編碼。如果符合的字串不是有效的編碼,則會被忽略。有效的編碼為 Latin-1
和 UTF-8
,其中的字元大小寫可以自由選擇。
範例
%% coding: utf-8
%% For this file we have chosen encoding = Latin-1
%% -*- coding: latin-1 -*-
錯誤資訊
ErrorInfo
是所有 I/O 模組傳回的標準 ErrorInfo
結構。格式如下:
{ErrorLine, Module, ErrorDescriptor}
使用以下呼叫取得描述錯誤的字串:
Module:format_error(ErrorDescriptor)
另請參閱
摘要
函式
關閉檔案的預處理。
傳回 Erlang 原始檔的預設編碼。
傳回編碼的字串表示形式。該字串可被 read_encoding/1,2
、read_encoding_from_binary/1,2
和 set_encoding/1,2
識別為有效的編碼。
接受 ErrorDescriptor
並傳回描述錯誤或警告的字串。此函式通常會在處理 ErrorInfo
結構時隱式呼叫(請參閱錯誤資訊章節)。
開啟檔案以進行預處理。
等同於 epp:open([{name, FileName}, {includes, IncludePath}])
。
等同於 epp:open([{name, FileName}, {includes, IncludePath}, {macros, PredefMacros}])
。
從已開啟的 Erlang 原始檔傳回下一個 Erlang 形式。檔案結尾會傳回元組 {eof, Location}
。第一個形式對應於隱式屬性 -file(File,1).
,其中 File
為檔案名稱。
預處理並剖析 Erlang 原始檔。請注意,在檔案結尾傳回的元組 {eof, Location}
會作為「形式」包含在內。
等同於 epp:parse_file(FileName, [{includes, IncludePath}, {macros, PredefMacros}])
。
從檔案讀取編碼。傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。
從二進位資料讀取編碼。傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。
從已開啟的 Erlang 原始檔傳回下一個 Erlang 形式的原始符記。檔案結尾會傳回元組 {eof, Line}
。第一個形式對應於隱式屬性 -file(File,1).
,其中 File
為檔案名稱。
預處理 Erlang 原始檔,傳回每個形式的原始符記列表的列表。請注意,在檔案結尾傳回的元組 {eof, Line}
會作為「形式」包含在內,並且掃描形式失敗的任何情況都會以元組 {error, ErrorInfo}
包含在列表中。
從 I/O 裝置讀取編碼,並據此設定裝置的編碼。由 File
參照的 I/O 裝置的位置不受影響。如果無法從 I/O 裝置讀取有效的編碼,則 I/O 裝置的編碼會設定為預設編碼。
類型
-type epp_handle() :: pid().
epp
伺服器的處理代碼。
-type source_encoding() :: latin1 | utf8.
-type warning_info() :: {erl_anno:location(), module(), term()}.
函式
-spec close(Epp) -> ok when Epp :: epp_handle().
關閉檔案的預處理。
-spec default_encoding() -> source_encoding().
傳回 Erlang 原始檔的預設編碼。
-spec encoding_to_string(Encoding) -> string() when Encoding :: source_encoding().
傳回編碼的字串表示形式。該字串可被 read_encoding/1,2
、read_encoding_from_binary/1,2
和 set_encoding/1,2
識別為有效的編碼。
-spec format_error(ErrorDescriptor) -> io_lib:chars() when ErrorDescriptor :: term().
接受 ErrorDescriptor
並傳回描述錯誤或警告的字串。此函式通常會在處理 ErrorInfo
結構時隱式呼叫(請參閱錯誤資訊章節)。
-spec open(Options) -> {ok, Epp} | {ok, Epp, Extra} | {error, ErrorDescriptor} when Options :: [{default_encoding, DefEncoding :: source_encoding()} | {includes, IncludePath :: [DirectoryName :: file:name()]} | {source_name, SourceName :: file:name()} | {deterministic, Enabled :: boolean()} | {macros, PredefMacros :: macros()} | {name, FileName :: file:name()} | {location, StartLocation :: erl_anno:location()} | {fd, FileDescriptor :: file:io_device()} | extra | {compiler_internal, [term()]}], Epp :: epp_handle(), Extra :: [{encoding, source_encoding() | none}], ErrorDescriptor :: term().
開啟檔案以進行預處理。
如果您想要變更預處理期間插入的隱式 -file() 屬性的檔案名稱,可以使用 {source_name, SourceName}
。如果未設定,則預設為開啟檔案的名稱。
設定 {deterministic, Enabled}
還會將預處理期間插入的隱式 -file() 屬性的檔案名稱縮減為僅路徑的基本名稱。
如果在 Options
中指定 extra
,則傳回值為 {ok, Epp, Extra}
而不是 {ok, Epp}
。
選項 location
會轉發給 Erlang 符記掃描器,請參閱 erl_scan:tokens/3,4
。
{compiler_internal,term()}
選項會轉發給 Erlang 符記掃描器,請參閱 {compiler_internal,term()}
。
-spec open(FileName, IncludePath) -> {ok, Epp} | {error, ErrorDescriptor} when FileName :: file:name(), IncludePath :: [DirectoryName :: file:name()], Epp :: epp_handle(), ErrorDescriptor :: term().
等同於 epp:open([{name, FileName}, {includes, IncludePath}])
。
-spec open(FileName, IncludePath, PredefMacros) -> {ok, Epp} | {error, ErrorDescriptor} when FileName :: file:name(), IncludePath :: [DirectoryName :: file:name()], PredefMacros :: macros(), Epp :: epp_handle(), ErrorDescriptor :: term().
等同於 epp:open([{name, FileName}, {includes, IncludePath}, {macros, PredefMacros}])
。
-spec parse_erl_form(Epp) -> {ok, AbsForm} | {error, ErrorInfo} | {warning, WarningInfo} | {eof, Location} when Epp :: epp_handle(), AbsForm :: erl_parse:abstract_form(), Location :: erl_anno:location(), ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), WarningInfo :: warning_info().
從已開啟的 Erlang 原始檔傳回下一個 Erlang 形式。檔案結尾會傳回元組 {eof, Location}
。第一個形式對應於隱式屬性 -file(File,1).
,其中 File
為檔案名稱。
-spec parse_file(FileName, Options) -> {ok, [Form]} | {ok, [Form], Extra} | {error, OpenError} when FileName :: file:name(), Options :: [{includes, IncludePath :: [DirectoryName :: file:name()]} | {source_name, SourceName :: file:name()} | {macros, PredefMacros :: macros()} | {default_encoding, DefEncoding :: source_encoding()} | {location, StartLocation :: erl_anno:location()} | {reserved_word_fun, Fun :: fun((atom()) -> boolean())} | {features, [Feature :: atom()]} | extra | {compiler_internal, [term()]}], Form :: erl_parse:abstract_form() | {error, ErrorInfo} | {eof, Location}, Location :: erl_anno:location(), ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), Extra :: [{encoding, source_encoding() | none}], OpenError :: file:posix() | badarg | system_limit.
預處理並剖析 Erlang 原始檔。請注意,在檔案結尾傳回的元組 {eof, Location}
會作為「形式」包含在內。
如果您想要變更預處理期間插入的隱式 -file() 屬性的檔案名稱,可以使用 {source_name, SourceName}
。如果未設定,則預設為開啟檔案的名稱。
如果在 Options
中指定 extra
,則傳回值為 {ok, [Form], Extra}
而不是 {ok, [Form]}
。
選項 location
會轉發給 Erlang 符記掃描器,請參閱 erl_scan:tokens/3,4
。
{compiler_internal,term()}
選項會轉發給 Erlang 符記掃描器,請參閱 {compiler_internal,term()}
。
-spec parse_file(FileName, IncludePath, PredefMacros) -> {ok, [Form]} | {error, OpenError} when FileName :: file:name(), IncludePath :: [DirectoryName :: file:name()], Form :: erl_parse:abstract_form() | {error, ErrorInfo} | {eof, Location}, PredefMacros :: macros(), Location :: erl_anno:location(), ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), OpenError :: file:posix() | badarg | system_limit.
等同於 epp:parse_file(FileName, [{includes, IncludePath}, {macros, PredefMacros}])
。
-spec read_encoding(FileName) -> source_encoding() | none when FileName :: file:name().
等同於 read_encoding/2
。
-spec read_encoding(FileName, Options) -> source_encoding() | none when FileName :: file:name(), Options :: [Option], Option :: {in_comment_only, boolean()}.
從檔案讀取編碼。傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。
選項 in_comment_only
預設為 true
,這對於 Erlang 原始檔是正確的。如果設定為 false
,則編碼字串不一定要出現在註解中。
-spec read_encoding_from_binary(Binary) -> source_encoding() | none when Binary :: binary().
-spec read_encoding_from_binary(Binary, Options) -> source_encoding() | none when Binary :: binary(), Options :: [Option], Option :: {in_comment_only, boolean()}.
從二進位資料讀取編碼。傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。
選項 in_comment_only
預設為 true
,這對於 Erlang 原始檔是正確的。如果設定為 false
,則編碼字串不一定要出現在註解中。
-spec scan_erl_form(Epp) -> {ok, Tokens} | {error, ErrorInfo} | {warning, WarningInfo} | {eof, Line} when Epp :: epp_handle(), Tokens :: erl_scan:tokens(), Line :: erl_anno:line(), ErrorInfo :: erl_scan:error_info() | erl_parse:error_info(), WarningInfo :: warning_info().
從已開啟的 Erlang 原始檔傳回下一個 Erlang 形式的原始符記。檔案結尾會傳回元組 {eof, Line}
。第一個形式對應於隱式屬性 -file(File,1).
,其中 File
為檔案名稱。
-spec scan_file(FileName, Options) -> {ok, [Form], Extra} | {error, OpenError} when FileName :: file:name(), Options :: [{includes, IncludePath :: [DirectoryName :: file:name()]} | {source_name, SourceName :: file:name()} | {macros, PredefMacros :: macros()} | {default_encoding, DefEncoding :: source_encoding()}], Form :: erl_scan:tokens() | {error, ErrorInfo} | {eof, Loc}, Loc :: erl_anno:location(), ErrorInfo :: erl_scan:error_info(), Extra :: [{encoding, source_encoding() | none}], OpenError :: file:posix() | badarg | system_limit.
預處理 Erlang 原始檔,傳回每個形式的原始符記列表的列表。請注意,在檔案結尾傳回的元組 {eof, Line}
會作為「形式」包含在內,並且掃描形式失敗的任何情況都會以元組 {error, ErrorInfo}
包含在列表中。
-spec set_encoding(File) -> source_encoding() | none when File :: io:device().
從 I/O 裝置讀取編碼,並據此設定裝置的編碼。由 File
參照的 I/O 裝置的位置不受影響。如果無法從 I/O 裝置讀取有效的編碼,則 I/O 裝置的編碼會設定為預設編碼。
傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。
-spec set_encoding(File, Default) -> source_encoding() | none when Default :: source_encoding(), File :: io:device().
從 I/O 裝置讀取編碼,並據此設定裝置的編碼。由 File
參照的 I/O 裝置的位置不受影響。如果無法從 I/O 裝置讀取有效的編碼,則 I/O 裝置的編碼會設定為 Default
指定的編碼。
傳回讀取的編碼,如果找不到有效的編碼則傳回 none
。