summaryrefslogtreecommitdiff
path: root/devdocs/go/go%2Fscanner%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/go/go%2Fscanner%2Findex.html
new repository
Diffstat (limited to 'devdocs/go/go%2Fscanner%2Findex.html')
-rw-r--r--devdocs/go/go%2Fscanner%2Findex.html111
1 files changed, 111 insertions, 0 deletions
diff --git a/devdocs/go/go%2Fscanner%2Findex.html b/devdocs/go/go%2Fscanner%2Findex.html
new file mode 100644
index 00000000..1b6566bc
--- /dev/null
+++ b/devdocs/go/go%2Fscanner%2Findex.html
@@ -0,0 +1,111 @@
+<h1> Package scanner </h1> <ul id="short-nav">
+<li><code>import "go/scanner"</code></li>
+<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
+<li><a href="#pkg-index" class="indexLink">Index</a></li>
+<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
+</ul> <h2 id="pkg-overview">Overview </h2> <p>Package scanner implements a scanner for Go source text. It takes a []byte as source which can then be tokenized through repeated calls to the Scan method. </p> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
+<li><a href="#PrintError">func PrintError(w io.Writer, err error)</a></li>
+<li><a href="#Error">type Error</a></li>
+<li> <a href="#Error.Error">func (e Error) Error() string</a>
+</li>
+<li><a href="#ErrorHandler">type ErrorHandler</a></li>
+<li><a href="#ErrorList">type ErrorList</a></li>
+<li> <a href="#ErrorList.Add">func (p *ErrorList) Add(pos token.Position, msg string)</a>
+</li>
+<li> <a href="#ErrorList.Err">func (p ErrorList) Err() error</a>
+</li>
+<li> <a href="#ErrorList.Error">func (p ErrorList) Error() string</a>
+</li>
+<li> <a href="#ErrorList.Len">func (p ErrorList) Len() int</a>
+</li>
+<li> <a href="#ErrorList.Less">func (p ErrorList) Less(i, j int) bool</a>
+</li>
+<li> <a href="#ErrorList.RemoveMultiples">func (p *ErrorList) RemoveMultiples()</a>
+</li>
+<li> <a href="#ErrorList.Reset">func (p *ErrorList) Reset()</a>
+</li>
+<li> <a href="#ErrorList.Sort">func (p ErrorList) Sort()</a>
+</li>
+<li> <a href="#ErrorList.Swap">func (p ErrorList) Swap(i, j int)</a>
+</li>
+<li><a href="#Mode">type Mode</a></li>
+<li><a href="#Scanner">type Scanner</a></li>
+<li> <a href="#Scanner.Init">func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode)</a>
+</li>
+<li> <a href="#Scanner.Scan">func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)</a>
+</li>
+</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_Scanner_Scan">Scanner.Scan</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>errors.go</span> <span>scanner.go</span> </p> <h2 id="PrintError">func <span>PrintError</span> </h2> <pre data-language="go">func PrintError(w io.Writer, err error)</pre> <p>PrintError is a utility function that prints a list of errors to w, one error per line, if the err parameter is an <a href="#ErrorList">ErrorList</a>. Otherwise it prints the err string. </p>
+<h2 id="Error">type <span>Error</span> </h2> <p>In an <a href="#ErrorList">ErrorList</a>, an error is represented by an *Error. The position Pos, if valid, points to the beginning of the offending token, and the error condition is described by Msg. </p>
+<pre data-language="go">type Error struct {
+ Pos token.Position
+ Msg string
+}
+</pre> <h3 id="Error.Error">func (Error) <span>Error</span> </h3> <pre data-language="go">func (e Error) Error() string</pre> <p>Error implements the error interface. </p>
+<h2 id="ErrorHandler">type <span>ErrorHandler</span> </h2> <p>An ErrorHandler may be provided to <a href="#Scanner.Init">Scanner.Init</a>. If a syntax error is encountered and a handler was installed, the handler is called with a position and an error message. The position points to the beginning of the offending token. </p>
+<pre data-language="go">type ErrorHandler func(pos token.Position, msg string)</pre> <h2 id="ErrorList">type <span>ErrorList</span> </h2> <p>ErrorList is a list of *Errors. The zero value for an ErrorList is an empty ErrorList ready to use. </p>
+<pre data-language="go">type ErrorList []*Error</pre> <h3 id="ErrorList.Add">func (*ErrorList) <span>Add</span> </h3> <pre data-language="go">func (p *ErrorList) Add(pos token.Position, msg string)</pre> <p>Add adds an <a href="#Error">Error</a> with given position and error message to an <a href="#ErrorList">ErrorList</a>. </p>
+<h3 id="ErrorList.Err">func (ErrorList) <span>Err</span> </h3> <pre data-language="go">func (p ErrorList) Err() error</pre> <p>Err returns an error equivalent to this error list. If the list is empty, Err returns nil. </p>
+<h3 id="ErrorList.Error">func (ErrorList) <span>Error</span> </h3> <pre data-language="go">func (p ErrorList) Error() string</pre> <p>An <a href="#ErrorList">ErrorList</a> implements the error interface. </p>
+<h3 id="ErrorList.Len">func (ErrorList) <span>Len</span> </h3> <pre data-language="go">func (p ErrorList) Len() int</pre> <p><a href="#ErrorList">ErrorList</a> implements the sort Interface. </p>
+<h3 id="ErrorList.Less">func (ErrorList) <span>Less</span> </h3> <pre data-language="go">func (p ErrorList) Less(i, j int) bool</pre> <h3 id="ErrorList.RemoveMultiples">func (*ErrorList) <span>RemoveMultiples</span> </h3> <pre data-language="go">func (p *ErrorList) RemoveMultiples()</pre> <p>RemoveMultiples sorts an <a href="#ErrorList">ErrorList</a> and removes all but the first error per line. </p>
+<h3 id="ErrorList.Reset">func (*ErrorList) <span>Reset</span> </h3> <pre data-language="go">func (p *ErrorList) Reset()</pre> <p>Reset resets an <a href="#ErrorList">ErrorList</a> to no errors. </p>
+<h3 id="ErrorList.Sort">func (ErrorList) <span>Sort</span> </h3> <pre data-language="go">func (p ErrorList) Sort()</pre> <p>Sort sorts an <a href="#ErrorList">ErrorList</a>. *<a href="#Error">Error</a> entries are sorted by position, other errors are sorted by error message, and before any *<a href="#Error">Error</a> entry. </p>
+<h3 id="ErrorList.Swap">func (ErrorList) <span>Swap</span> </h3> <pre data-language="go">func (p ErrorList) Swap(i, j int)</pre> <h2 id="Mode">type <span>Mode</span> </h2> <p>A mode value is a set of flags (or 0). They control scanner behavior. </p>
+<pre data-language="go">type Mode uint</pre> <pre data-language="go">const (
+ ScanComments Mode = 1 &lt;&lt; iota // return comments as COMMENT tokens
+
+)</pre> <h2 id="Scanner">type <span>Scanner</span> </h2> <p>A Scanner holds the scanner's internal state while processing a given text. It can be allocated as part of another data structure but must be initialized via <a href="#Scanner.Init">Scanner.Init</a> before use. </p>
+<pre data-language="go">type Scanner struct {
+
+ // public state - ok to modify
+ ErrorCount int // number of errors encountered
+ // contains filtered or unexported fields
+}
+</pre> <h3 id="Scanner.Init">func (*Scanner) <span>Init</span> </h3> <pre data-language="go">func (s *Scanner) Init(file *token.File, src []byte, err ErrorHandler, mode Mode)</pre> <p>Init prepares the scanner s to tokenize the text src by setting the scanner at the beginning of src. The scanner uses the file set file for position information and it adds line information for each line. It is ok to re-use the same file when re-scanning the same file as line information which is already present is ignored. Init causes a panic if the file size does not match the src size. </p>
+<p>Calls to <a href="#Scanner.Scan">Scanner.Scan</a> will invoke the error handler err if they encounter a syntax error and err is not nil. Also, for each error encountered, the <a href="#Scanner">Scanner</a> field ErrorCount is incremented by one. The mode parameter determines how comments are handled. </p>
+<p>Note that Init may call err if there is an error in the first character of the file. </p>
+<h3 id="Scanner.Scan">func (*Scanner) <span>Scan</span> </h3> <pre data-language="go">func (s *Scanner) Scan() (pos token.Pos, tok token.Token, lit string)</pre> <p>Scan scans the next token and returns the token position, the token, and its literal string if applicable. The source end is indicated by <span>token.EOF</span>. </p>
+<p>If the returned token is a literal (<span>token.IDENT</span>, <span>token.INT</span>, <span>token.FLOAT</span>, <span>token.IMAG</span>, <span>token.CHAR</span>, <span>token.STRING</span>) or <span>token.COMMENT</span>, the literal string has the corresponding value. </p>
+<p>If the returned token is a keyword, the literal string is the keyword. </p>
+<p>If the returned token is <span>token.SEMICOLON</span>, the corresponding literal string is ";" if the semicolon was present in the source, and "\n" if the semicolon was inserted because of a newline or at EOF. </p>
+<p>If the returned token is <span>token.ILLEGAL</span>, the literal string is the offending character. </p>
+<p>In all other cases, Scan returns an empty literal string. </p>
+<p>For more tolerant parsing, Scan will return a valid token if possible even if a syntax error was encountered. Thus, even if the resulting token sequence contains no illegal tokens, a client may not assume that no error occurred. Instead it must check the scanner's ErrorCount or the number of calls of the error handler, if there was one installed. </p>
+<p>Scan adds line information to the file added to the file set with Init. Token positions are relative to that file and thus relative to the file set. </p> <h4 id="example_Scanner_Scan"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">// src is the input that we want to tokenize.
+src := []byte("cos(x) + 1i*sin(x) // Euler")
+
+// Initialize the scanner.
+var s scanner.Scanner
+fset := token.NewFileSet() // positions are relative to fset
+file := fset.AddFile("", fset.Base(), len(src)) // register input "file"
+s.Init(file, src, nil /* no error handler */, scanner.ScanComments)
+
+// Repeated calls to Scan yield the token sequence found in the input.
+for {
+ pos, tok, lit := s.Scan()
+ if tok == token.EOF {
+ break
+ }
+ fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
+}
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">1:1 IDENT "cos"
+1:4 ( ""
+1:5 IDENT "x"
+1:6 ) ""
+1:8 + ""
+1:10 IMAG "1i"
+1:12 * ""
+1:13 IDENT "sin"
+1:16 ( ""
+1:17 IDENT "x"
+1:18 ) ""
+1:20 COMMENT "// Euler"
+1:28 ; "\n"
+</pre><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
+ <a href="http://golang.org/pkg/go/scanner/" class="_attribution-link">http://golang.org/pkg/go/scanner/</a>
+ </p>
+</div>