Skip to content

Commit 4fc4f37

Browse files
committed
testparser: make it compile too and make a py3 comparison
1 parent c8b83ac commit 4fc4f37

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

Diff for: parser/testparser/py3compile.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Compile any files passed in on the command line
4+
"""
5+
6+
import sys
7+
for path in sys.argv[1:]:
8+
print("Compiling %s" % path)
9+
with open(path) as f:
10+
try:
11+
data = f.read()
12+
compile(data, path, "exec")
13+
except Exception as e:
14+
print("Failed: %s" % e)

Diff for: parser/testparser/test.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/bin/bash
22
# Run testparser over python3 source
3+
#
4+
# Pass in args to be passed to testparser, eg -c, -l
5+
# Parses by default
36

47
PY3SOURCE=~/Code/cpython
58

69
go install
710

8-
find $PY3SOURCE -type f -name \*.py | grep -v "lib2to3/tests" | xargs testparser
11+
# Grep out python2 source code which we can't parse and files with deliberate syntax errors
12+
find $PY3SOURCE -type f -name \*.py | egrep -v "Lib/(lib2to3/tests|test/bad.*py)|Tools/(hg|msi|test2to3)/" | xargs testparser "$@"
13+
14+
#find $PY3SOURCE -type f -name \*.py | egrep -v "Lib/(lib2to3/tests|test/bad.*py)|Tools/(hg|msi|test2to3)/" | xargs ./py3compile.py "$@"
15+

Diff for: parser/testparser/testparser.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,57 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"io/ioutil"
67
"log"
78
"os"
89

10+
"github.com/ncw/gpython/compile"
911
"github.com/ncw/gpython/parser"
1012
)
1113

1214
var (
13-
lex = flag.Bool("l", false, "Lex the file only")
14-
debugLevel = flag.Int("d", 0, "Debug level 0-4")
15+
lexFile = flag.Bool("l", false, "Lex the file only")
16+
compileFile = flag.Bool("c", false, "Lex, Parse and compile the file")
17+
debugLevel = flag.Int("d", 0, "Debug level 0-4")
1518
)
1619

1720
func main() {
1821
flag.Parse()
1922
parser.SetDebug(*debugLevel)
23+
if len(flag.Args()) == 0 {
24+
log.Printf("Need files to parse")
25+
os.Exit(1)
26+
}
2027
for _, path := range flag.Args() {
21-
if *lex {
28+
if *lexFile {
2229
fmt.Printf("Lexing %q\n", path)
30+
} else if *compileFile {
31+
fmt.Printf("Compiling %q\n", path)
2332
} else {
2433
fmt.Printf("Parsing %q\n", path)
2534
}
2635
in, err := os.Open(path)
2736
if err != nil {
2837
log.Fatal(err)
2938
}
30-
fmt.Printf("-----------------\n")
31-
if *lex {
32-
_, err = parser.Lex(in, "exec")
39+
if *debugLevel > 0 {
40+
fmt.Printf("-----------------\n")
41+
}
42+
if *lexFile {
43+
_, err = parser.Lex(in, path, "exec")
44+
} else if *compileFile {
45+
var input []byte
46+
input, err = ioutil.ReadAll(in)
47+
if err != nil {
48+
log.Fatalf("Failed to read %q: %v", path, err)
49+
}
50+
_, err = compile.Compile(string(input), path, "exec", 0, false)
3351
} else {
34-
_, err = parser.Parse(in, "exec")
52+
_, err = parser.Parse(in, path, "exec")
53+
}
54+
if *debugLevel > 0 {
55+
fmt.Printf("-----------------\n")
3556
}
36-
fmt.Printf("-----------------\n")
3757
closeErr := in.Close()
3858
if err != nil {
3959
log.Fatalf("Failed on %q: %v", path, err)

0 commit comments

Comments
 (0)