scanner

package
v0.7.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2021 License: BSD-1-Clause, BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package scanner implements a scanner for N-Quads[0] source text. It takes a []byte as source which can then be tokenized through repeated calls to the Scan method.

Index

Examples

Constants

View Source
const (
	ILLEGAL // Returned when no token was recognized.
	EOF     // Returned after all source was consumed.

	DOT     // .
	DACCENT // ^^
	LABEL   // [1]
	EOL     // [2]
	IRIREF  // [3]
	LANGTAG // [4]
	STRING  // [5]
)

Values of type Token.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scanner

type Scanner struct {
	Col    int     // Starting column of the last scanned token.
	Errors []error // List of accumulated errors.
	Fname  string  // File name (reported) of the scanned source.
	Line   int     // Starting line of the last scanned token.
	NCol   int     // Starting column (reported) for the next scanned token.
	NLine  int     // Starting line (reported) for the next scanned token.
	// contains filtered or unexported fields
}

A Scanner holds the scanner's internal state while processing a given text.

func New

func New(fname string, src []byte) (s *Scanner)

New returns a newly created Scanner with fname as the name of the source.

func (*Scanner) Error

func (s *Scanner) Error(msg string)

Error implements yyLexer.

func (*Scanner) Pos

func (s *Scanner) Pos() int

Pos returns the starting offset of the last scanned token.

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token, st string)

Scan scans the next token and returns the token and its value if applicable. The source end is indicated by EOF.

If the returned token is ILLEGAL, the literal string is the offending character or number/string/char literal.

Example
const src = `

<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> @us-EN <http://example.org/graph3> . # comments here
# or on a line by themselves
_:subject1 <http://an.example/predicate1> "object\u00411" "cafe\u0301 \'time" <http://example.org/graph1> .
_:subject2 <http://an.example/predicate2> "object\U000000422"  ^^ <http://example.com/literal> <http://example.org/graph5> .
_:0 _:01
 _:0 _:01

`
sc := New("test", []byte(src))
for {
	tok, val := sc.Scan()
	fmt.Printf("%s:%d:%d %v %q\n", sc.Fname, sc.Line, sc.Col, tok, val)
	if tok == EOF {
		break
	}
}
fmt.Printf("%v", sc.Errors)
Output:

test:2:0 EOL ""
test:3:1 IRIREF "http://one.example/subject1"
test:3:31 IRIREF "http://one.example/predicate1"
test:3:63 IRIREF "http://one.example/object1"
test:3:92 LANGTAG "us-EN"
test:3:99 IRIREF "http://example.org/graph3"
test:3:127 DOT "."
test:4:0 EOL ""
test:5:0 EOL ""
test:5:1 LABEL "subject1"
test:5:12 IRIREF "http://an.example/predicate1"
test:5:43 STRING "objectA1"
test:5:59 STRING "café 'time"
test:5:79 IRIREF "http://example.org/graph1"
test:5:107 DOT "."
test:6:0 EOL ""
test:6:1 LABEL "subject2"
test:6:12 IRIREF "http://an.example/predicate2"
test:6:43 STRING "objectB2"
test:6:64 DACCENT "^^"
test:6:67 IRIREF "http://example.com/literal"
test:6:96 IRIREF "http://example.org/graph5"
test:6:124 DOT "."
test:7:0 EOL ""
test:7:1 LABEL "0"
test:7:5 LABEL "01"
test:8:0 EOL ""
test:8:2 LABEL "0"
test:8:6 LABEL "01"
test:9:0 EOL ""
test:10:1 EOF ""
[]

type Token

type Token int

Token is the type of the token identifier returned by Scan().

func (Token) String

func (i Token) String() string

String implements fmt.Stringer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL