2008-07-02 18:02:55 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import Scanner "scanner"
|
|
|
|
|
|
|
|
|
2008-08-06 19:57:37 -06:00
|
|
|
func Scan1(filename, src string) {
|
2008-07-02 18:02:55 -06:00
|
|
|
S := new(Scanner.Scanner);
|
2008-07-10 15:42:33 -06:00
|
|
|
S.Open(filename, src);
|
2008-07-02 18:02:55 -06:00
|
|
|
for {
|
2008-07-18 18:18:29 -06:00
|
|
|
tok, pos, val := S.Scan();
|
2008-08-11 22:20:42 -06:00
|
|
|
print(pos, ": ", Scanner.TokenName(tok));
|
2008-08-05 19:52:37 -06:00
|
|
|
if tok == Scanner.IDENT || tok == Scanner.INT || tok == Scanner.FLOAT || tok == Scanner.STRING {
|
2008-08-11 22:20:42 -06:00
|
|
|
print(" ", val);
|
2008-07-18 18:18:29 -06:00
|
|
|
}
|
2008-08-11 22:20:42 -06:00
|
|
|
print("\n");
|
2008-07-02 18:02:55 -06:00
|
|
|
if tok == Scanner.EOF {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-06 19:57:37 -06:00
|
|
|
func Scan2(filename, src string) {
|
|
|
|
S := new(Scanner.Scanner);
|
|
|
|
S.Open(filename, src);
|
|
|
|
c := new(chan *Scanner.Token, 32);
|
|
|
|
go S.Server(c);
|
|
|
|
for {
|
|
|
|
var t *Scanner.Token;
|
|
|
|
t = <- c;
|
|
|
|
tok, pos, val := t.tok, t.pos, t.val;
|
2008-08-11 22:20:42 -06:00
|
|
|
print(pos, ": ", Scanner.TokenName(tok));
|
2008-08-06 19:57:37 -06:00
|
|
|
if tok == Scanner.IDENT || tok == Scanner.INT || tok == Scanner.FLOAT || tok == Scanner.STRING {
|
2008-08-11 22:20:42 -06:00
|
|
|
print(" ", val);
|
2008-08-06 19:57:37 -06:00
|
|
|
}
|
2008-08-11 22:20:42 -06:00
|
|
|
print("\n");
|
2008-08-06 19:57:37 -06:00
|
|
|
if tok == Scanner.EOF {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-02 18:02:55 -06:00
|
|
|
func main() {
|
|
|
|
for i := 1; i < sys.argc(); i++ {
|
|
|
|
var src string;
|
|
|
|
var ok bool;
|
|
|
|
src, ok = sys.readfile(sys.argv(i));
|
|
|
|
if ok {
|
2008-08-11 22:20:42 -06:00
|
|
|
print("scanning (standard) " + sys.argv(i) + "\n");
|
2008-08-06 19:57:37 -06:00
|
|
|
Scan1(sys.argv(i), src);
|
2008-08-11 22:20:42 -06:00
|
|
|
print("\n");
|
|
|
|
print("scanning (channels) " + sys.argv(i) + "\n");
|
2008-08-06 19:57:37 -06:00
|
|
|
Scan2(sys.argv(i), src);
|
2008-07-02 18:02:55 -06:00
|
|
|
} else {
|
2008-08-11 22:20:42 -06:00
|
|
|
print("error: cannot read " + sys.argv(i) + "\n");
|
2008-07-02 18:02:55 -06:00
|
|
|
}
|
2008-08-11 22:20:42 -06:00
|
|
|
print("\n");
|
2008-07-02 18:02:55 -06:00
|
|
|
}
|
|
|
|
}
|