📄 glib-lexical-scanner.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Lexical Scanner</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-Miscellaneous-Utility-Functions.html" title="Miscellaneous Utility Functions"><link rel="next" href="glib-Automatic-String-Completion.html" title="Automatic String Completion"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Miscellaneous-Utility-Functions.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Automatic-String-Completion.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Lexical-Scanner"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Lexical Scanner</span></h2><p>Lexical Scanner — a general purpose lexical scanner.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Lexical-Scanner.html#GScanner">GScanner</a>;<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a>* <a href="glib-Lexical-Scanner.html#g-scanner-new">g_scanner_new</a> (const <a href="glib-Lexical-Scanner.html#GScannerConfig">GScannerConfig</a> *config_templ); <a href="glib-Lexical-Scanner.html#GScannerConfig">GScannerConfig</a>;void <a href="glib-Lexical-Scanner.html#g-scanner-input-file">g_scanner_input_file</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#gint">gint</a> input_fd);void <a href="glib-Lexical-Scanner.html#g-scanner-sync-file-offset">g_scanner_sync_file_offset</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);void <a href="glib-Lexical-Scanner.html#g-scanner-input-text">g_scanner_input_text</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, const <a href="glib-Basic-Types.html#gchar">gchar</a> *text, <a href="glib-Basic-Types.html#guint">guint</a> text_len);<a href="glib-Lexical-Scanner.html#GTokenType">GTokenType</a> <a href="glib-Lexical-Scanner.html#g-scanner-peek-next-token">g_scanner_peek_next_token</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Lexical-Scanner.html#GTokenType">GTokenType</a> <a href="glib-Lexical-Scanner.html#g-scanner-get-next-token">g_scanner_get_next_token</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-Lexical-Scanner.html#g-scanner-cur-line">g_scanner_cur_line</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-Lexical-Scanner.html#g-scanner-cur-position">g_scanner_cur_position</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Lexical-Scanner.html#GTokenType">GTokenType</a> <a href="glib-Lexical-Scanner.html#g-scanner-cur-token">g_scanner_cur_token</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Lexical-Scanner.html#GTokenValue">GTokenValue</a> <a href="glib-Lexical-Scanner.html#g-scanner-cur-value">g_scanner_cur_value</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-Lexical-Scanner.html#g-scanner-eof">g_scanner_eof</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-Lexical-Scanner.html#g-scanner-set-scope">g_scanner_set_scope</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#guint">guint</a> scope_id);void <a href="glib-Lexical-Scanner.html#g-scanner-scope-add-symbol">g_scanner_scope_add_symbol</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#guint">guint</a> scope_id, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol, <a href="glib-Basic-Types.html#gpointer">gpointer</a> value);void <a href="glib-Lexical-Scanner.html#g-scanner-scope-foreach-symbol">g_scanner_scope_foreach_symbol</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#guint">guint</a> scope_id, <a href="glib-Hash-Tables.html#GHFunc">GHFunc</a> func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a href="glib-Basic-Types.html#gpointer">gpointer</a> <a href="glib-Lexical-Scanner.html#g-scanner-scope-lookup-symbol">g_scanner_scope_lookup_symbol</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#guint">guint</a> scope_id, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol);void <a href="glib-Lexical-Scanner.html#g-scanner-scope-remove-symbol">g_scanner_scope_remove_symbol</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#guint">guint</a> scope_id, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol);#define <a href="glib-Lexical-Scanner.html#g-scanner-freeze-symbol-table">g_scanner_freeze_symbol_table</a> (scanner)#define <a href="glib-Lexical-Scanner.html#g-scanner-thaw-symbol-table">g_scanner_thaw_symbol_table</a> (scanner)<a href="glib-Basic-Types.html#gpointer">gpointer</a> <a href="glib-Lexical-Scanner.html#g-scanner-lookup-symbol">g_scanner_lookup_symbol</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol);void <a href="glib-Lexical-Scanner.html#g-scanner-warn">g_scanner_warn</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, const <a href="glib-Basic-Types.html#gchar">gchar</a> *format, ...);void <a href="glib-Lexical-Scanner.html#g-scanner-error">g_scanner_error</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, const <a href="glib-Basic-Types.html#gchar">gchar</a> *format, ...);void <a href="glib-Lexical-Scanner.html#g-scanner-unexp-token">g_scanner_unexp_token</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Lexical-Scanner.html#GTokenType">GTokenType</a> expected_token, const <a href="glib-Basic-Types.html#gchar">gchar</a> *identifier_spec, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol_spec, const <a href="glib-Basic-Types.html#gchar">gchar</a> *symbol_name, const <a href="glib-Basic-Types.html#gchar">gchar</a> *message, <a href="glib-Basic-Types.html#gint">gint</a> is_error);void (<a href="glib-Lexical-Scanner.html#GScannerMsgFunc">*GScannerMsgFunc</a>) (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner, <a href="glib-Basic-Types.html#gchar">gchar</a> *message, <a href="glib-Basic-Types.html#gboolean">gboolean</a> error);void <a href="glib-Lexical-Scanner.html#g-scanner-destroy">g_scanner_destroy</a> (<a href="glib-Lexical-Scanner.html#GScanner">GScanner</a> *scanner);enum <a href="glib-Lexical-Scanner.html#GTokenType">GTokenType</a>;union <a href="glib-Lexical-Scanner.html#GTokenValue">GTokenValue</a>;enum <a href="glib-Lexical-Scanner.html#GErrorType">GErrorType</a>;#define <a href="glib-Lexical-Scanner.html#G-CSET-a-2-z">G_CSET_a_2_z</a>#define <a href="glib-Lexical-Scanner.html#G-CSET-A-2-Z:CAPS">G_CSET_A_2_Z</a>#define <a href="glib-Lexical-Scanner.html#G-CSET-DIGITS:CAPS">G_CSET_DIGITS</a>#define <a href="glib-Lexical-Scanner.html#G-CSET-LATINC:CAPS">G_CSET_LATINC</a>#define <a href="glib-Lexical-Scanner.html#G-CSET-LATINS:CAPS">G_CSET_LATINS</a>#define <a href="glib-Lexical-Scanner.html#g-scanner-add-symbol">g_scanner_add_symbol</a> ( scanner, symbol, value )#define <a href="glib-Lexical-Scanner.html#g-scanner-remove-symbol">g_scanner_remove_symbol</a> ( scanner, symbol )#define <a href="glib-Lexical-Scanner.html#g-scanner-foreach-symbol">g_scanner_foreach_symbol</a> ( scanner, func, data )</pre></div><div class="refsect1" lang="en"><a name="id3029131"></a><h2>Description</h2><p>The <a href="glib-Lexical-Scanner.html#GScanner"><span class="type">GScanner</span></a> and its associated functions provide a general purposelexical scanner.</p><p>FIXME: really needs an example and more detail, but I don't completelyunderstand it myself. Look at gtkrc.c for some code using the scanner.</p></div><div class="refsect1" lang="en"><a name="id3029156"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3029161"></a><h3><a name="GScanner"></a>GScanner</h3><a class="indexterm" name="id3029171"></a><pre class="programlisting">typedef struct { /* unused fields */ gpointer user_data; guint max_parse_errors; /* g_scanner_error() increments this field */ guint parse_errors; /* name of input stream, featured by the default message handler */ const gchar *input_name; /* quarked data */ GData *qdata; /* link into the scanner configuration */ GScannerConfig *config; /* fields filled in after g_scanner_get_next_token() */ GTokenType token; GTokenValue value; guint line; guint position; /* fields filled in after g_scanner_peek_next_token() */ GTokenType next_token; GTokenValue next_value; guint next_line; guint next_position; /* to be considered private */ GHashTable *symbol_table; gint input_fd; const gchar *text; const gchar *text_end; gchar *buffer; guint scope_id; /* handler function for _warn and _error */ GScannerMsgFunc msg_handler;} GScanner;</pre><p>The data structure representing a lexical scanner.</p><p>You should set <em class="structfield"><code>input_name</code></em> after creating the scanner, since it is used by the default message handler when displaying warnings and errors. If you are scanning a file, the file name would be a good choice.</p><p>The <em class="structfield"><code>user_data</code></em> and<em class="structfield"><code>max_parse_errors</code></em> fields are not used.If you need to associate extra data with the scanner you can place them here.</p><p>If you want to use your own message handler you can set the<em class="structfield"><code>msg_handler</code></em> field. The type of the messagehandler function is declared by <a href="glib-Lexical-Scanner.html#GScannerMsgFunc"><span class="type">GScannerMsgFunc</span></a>.</p></div><hr><div class="refsect2" lang="en"><a name="id3029243"></a><h3><a name="g-scanner-new"></a>g_scanner_new ()</h3><a class="indexterm" name="id3029254"></a><pre class="programlisting"><a href="glib-Lexical-Scanner.html#GScanner">GScanner</a>* g_scanner_new (const <a href="glib-Lexical-Scanner.html#GScannerConfig">GScannerConfig</a> *config_templ);</pre><p>Creates a new <a href="glib-Lexical-Scanner.html#GScanner"><span class="type">GScanner</span></a>.The <em class="parameter"><code>config_templ</code></em> structure specifies the initial settings of the scanner,which are copied into the <a href="glib-Lexical-Scanner.html#GScanner"><span class="type">GScanner</span></a> <em class="structfield"><code>config</code></em> field.If you pass <code class="literal">NULL</code> then the default settings are used.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>config_templ</code></em> :</span></td><td>the initial scanner settings.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Lexical-Scanner.html#GScanner"><span class="type">GScanner</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3029353"></a><h3><a name="GScannerConfig"></a>GScannerConfig</h3><a class="indexterm" name="id3029364"></a><pre class="programlisting">typedef struct { /* Character sets */ gchar *cset_skip_characters; /* default: " \t\n" */ gchar *cset_identifier_first; gchar *cset_identifier_nth; gchar *cpair_comment_single; /* default: "#\n" */ /* Should symbol lookup work case sensitive? */ guint case_sensitive : 1; /* Boolean values to be adjusted "on the fly" * to configure scanning behaviour. */ guint skip_comment_multi : 1; /* C like comment */ guint skip_comment_single : 1; /* single line comment */ guint scan_comment_multi : 1; /* scan multi line comments? */ guint scan_identifier : 1; guint scan_identifier_1char : 1; guint scan_identifier_NULL : 1; guint scan_symbols : 1; guint scan_binary : 1; guint scan_octal : 1; guint scan_float : 1; guint scan_hex : 1; /* `0x0ff0' */ guint scan_hex_dollar : 1; /* `$0ff0' */ guint scan_string_sq : 1; /* string: 'anything' */ guint scan_string_dq : 1; /* string: "\\-escapes!\n" */ guint numbers_2_int : 1; /* bin, octal, hex => int */ guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */ guint identifier_2_string : 1; guint char_2_token : 1; /* return G_TOKEN_CHAR? */ guint symbol_2_token : 1; guint scope_0_fallback : 1; /* try scope 0 on lookups? */ guint store_int64 : 1; /* use value.v_int64 rather than v_int */ guint padding_dummy;} GScannerConfig;</pre><p>Specifies the <a href="glib-Lexical-Scanner.html#GScanner"><span class="type">GScanner</span></a> settings.</p><p><em class="structfield"><code>cset_skip_characters</code></em> specifies which charactersshould be skipped by the scanner (the default is the whitespace characters:space, tab, carriage-return and line-feed).</p><p><em class="structfield"><code>cset_identifier_first</code></em> specifies the characterswhich can start identifiers (the default is <a href="glib-Lexical-Scanner.html#G-CSET-a-2-z"><span class="type">G_CSET_a_2_z</span></a>, "_", and <a href="glib-Lexical-Scanner.html#G-CSET-A-2-Z:CAPS"><span class="type">G_CSET_A_2_Z</span></a>).</p><p><em class="structfield"><code>cset_identifier_nth</code></em> specifies the characterswhich can be used in identifiers, after the first character (the default is <a href="glib-Lexical-Scanner.html#G-CSET-a-2-z"><span class="type">G_CSET_a_2_z</span></a>, "_0123456789", <a href="glib-Lexical-Scanner.html#G-CSET-A-2-Z:CAPS"><span class="type">G_CSET_A_2_Z</span></a>, <a href="glib-Lexical-Scanner.html#G-CSET-LATINS:CAPS"><span class="type">G_CSET_LATINS</span></a>,<a href="glib-Lexical-Scanner.html#G-CSET-LATINC:CAPS"><span class="type">G_CSET_LATINC</span></a>).</p><p><em class="structfield"><code>cpair_comment_single</code></em> specifies the charactersat the start and end of single-line comments. The default is "#\n" whichmeans that single-line comments start with a '#' and continue until a '\n'(end of line).</p><p><em class="structfield"><code>case_sensitive</code></em> specifies if symbols arecase sensitive (the default is <code class="literal">FALSE</code>).</p><p><em class="structfield"><code>skip_comment_multi</code></em> specifies if multi-linecomments are skipped and not returned as tokens (the default is <code class="literal">TRUE</code>).</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -