Example #1
0
File: parser.go Project: 8l/leaf
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, isComment)

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

	files := fset.Args()

	if len(files) == 0 {
		fmt.Fprintln(os.Stderr, "no input file.")
		return
	}

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

	for _, f := range files {
		fmt.Printf("[%s]\n", f)

		fin, e := os.Open(f)
		if e != nil {
			onError(e)
			continue
		}

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

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

		onError(fin.Close())
	}
}