Skip to content
A simple lexical analyzer for Go
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis.yml
LICENSE
README.md
doc.go
errors.go
errors_test.go
example_test.go
lexer.go
lexer_test.go
position.go
position_test.go
token.go
token_test.go

README.md

simplexer

Build Status Test Coverage Maintainability GoDoc

A simple lexical analyzser for Go.

example

simplest usage

package main

import (
	"fmt"
	"strings"

	"github.com/macrat/simplexer"
)

func Example() {
	input := "hello_world = \"hello world\"\nnumber = 1"
	lexer := simplexer.NewLexer(strings.NewReader(input))

	fmt.Println(input)
	fmt.Println("==========")

	for {
		token, err := lexer.Scan()
		if err != nil {
			panic(err.Error())
		}
		if token == nil {
			fmt.Println("==========")
			return
		}

		fmt.Printf("line %2d, column %2d: %s: %s\n",
			token.Position.Line,
			token.Position.Column,
			token.Type,
			token.Literal)
	}
}

It is output as follow.

hello_world = "hello world"
number = 1
==========
line  0, column  0: IDENT: hello_world
line  0, column 12: OTHER: =
line  0, column 14: STRING: "hello world"
line  1, column  0: IDENT: number
line  1, column  7: OTHER: =
line  1, column  9: NUMBER: 1
==========

more examples

Please see godoc.

You can’t perform that action at this time.