/
main.go
63 lines (55 loc) · 954 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"bufio"
"flag"
"fmt"
"go/scanner"
"go/token"
"io"
"os"
)
func main () {
// read
flag.Parse()
filename := flag.Arg(0)
fptr, err := os.Open(filename)
if err != nil { panic(err) }
frdr := bufio.NewReaderSize(fptr, 0x1000000)
src, err := frdr.ReadSlice('\x7F')
if err != nil {
if err != io.EOF {
panic(err)
}
}
// scan
var s scanner.Scanner
fset := token.NewFileSet()
file := fset.AddFile(filename, fset.Base(), len(src))
s.Init(file, src, nil, scanner.ScanComments)
for {
_, tok, lit := s.Scan()
if tok == token.EOF {
break
}
switch tok {
case token.SEMICOLON:
fmt.Print(";")
case token.COMMENT:
continue
case token.IDENT:
fallthrough
case token.INT:
fallthrough
case token.FLOAT:
fallthrough
case token.IMAG:
fallthrough
case token.CHAR:
fallthrough
case token.STRING:
fmt.Printf("%s ", lit)
default:
fmt.Printf("%s ", tok.String())
}
}
}