Example #1
0
File: main.go Project: 8l/leaf
func mainLex(args []string) {
	fset := flag.NewFlagSet("asm8-lex", flag.ExitOnError)
	fset.Parse(args)

	files := fset.Args()

	if len(files) != 1 {
		fmt.Fprintln(os.Stderr, "expects one single input file.")
		os.Exit(1)
	}

	onError := func(e error) { fmt.Fprintln(os.Stderr, e) }

	f := files[0]
	fin, e := os.Open(f)
	if e != nil {
		onError(e)
		os.Exit(1)
	}

	lex := lexer.New(fin, f)
	lex.OnError(onError)

	for lex.Scan() {
		tok := lex.Token()
		fmt.Println(tok.String())
	}

	e = fin.Close()
	if e != nil {
		onError(e)
	}
}
Example #2
0
File: parser.go Project: 8l/leaf
// New creates a new parser that parses a file into an AST.
func New(in io.Reader, filename string) *Parser {
	ret := new(Parser)
	ret.filename = filename
	ret.lx = lexer.New(in, filename)
	ret.s = lexin.NewScanner(ret.lx, func(t *tok.Token) bool {
		return t.Is(tt.Comment)
	})

	return ret
}