Commit aa126447 authored by Robert Griesemer's avatar Robert Griesemer

- added mechanism to detect capitalization issues

Use: pretty -naming files

R=r
OCL=22859
CL=22859
parent 61f33020
...@@ -6,11 +6,13 @@ package AST ...@@ -6,11 +6,13 @@ package AST
import ( import (
"array"; "array";
"utf8";
"unicode";
Scanner "scanner"; Scanner "scanner";
) )
type ( export type (
Object struct; Object struct;
Type struct; Type struct;
...@@ -65,6 +67,15 @@ export type Object struct { ...@@ -65,6 +67,15 @@ export type Object struct {
} }
func (obj *Object) IsExported() bool {
switch obj.kind {
case NONE /* FUNC for now */, CONST, TYPE, VAR, FUNC:
ch, size := utf8.DecodeRuneInString(obj.ident, 0);
return unicode.IsUpper(ch);
}
return false;
}
export var Universe_void_typ *Type // initialized by Universe to Universe.void_typ export var Universe_void_typ *Type // initialized by Universe to Universe.void_typ
var ObjectId int; var ObjectId int;
......
...@@ -30,6 +30,7 @@ export type Flags struct { ...@@ -30,6 +30,7 @@ export type Flags struct {
columns bool; columns bool;
testmode bool; testmode bool;
tokenchan bool; tokenchan bool;
naming bool;
} }
...@@ -90,7 +91,8 @@ func (h *ErrorHandler) ErrorMsg(pos int, msg string) { ...@@ -90,7 +91,8 @@ func (h *ErrorHandler) ErrorMsg(pos int, msg string) {
h.errpos = pos; h.errpos = pos;
if h.nerrors >= 10 { if h.nerrors >= 10 {
sys.exit(1); // TODO enable when done with name convention
//sys.exit(1);
} }
} }
...@@ -134,7 +136,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) { ...@@ -134,7 +136,7 @@ export func Compile(src_file string, flags *Flags) (*AST.Program, int) {
} }
var parser Parser.Parser; var parser Parser.Parser;
parser.Open(flags.verbose, flags.sixg, flags.deps, &scanner, tstream); parser.Open(flags.verbose, flags.sixg, flags.deps, flags.naming, &scanner, tstream);
prog := parser.ParseProgram(); prog := parser.ParseProgram();
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
export type Parser struct { export type Parser struct {
// Tracing/debugging // Tracing/debugging
verbose, sixg, deps bool; verbose, sixg, deps, naming bool;
indent uint; indent uint;
// Scanner // Scanner
...@@ -109,10 +109,11 @@ func (P *Parser) Next() { ...@@ -109,10 +109,11 @@ func (P *Parser) Next() {
} }
func (P *Parser) Open(verbose, sixg, deps bool, scanner *Scanner.Scanner, tokchan <-chan *Scanner.Token) { func (P *Parser) Open(verbose, sixg, deps, naming bool, scanner *Scanner.Scanner, tokchan <-chan *Scanner.Token) {
P.verbose = verbose; P.verbose = verbose;
P.sixg = sixg; P.sixg = sixg;
P.deps = deps; P.deps = deps;
P.naming = naming;
P.indent = 0; P.indent = 0;
P.scanner = scanner; P.scanner = scanner;
...@@ -191,6 +192,33 @@ func (P *Parser) Declare(p *AST.Expr, kind int) { ...@@ -191,6 +192,33 @@ func (P *Parser) Declare(p *AST.Expr, kind int) {
} }
func (P *Parser) VerifyExport1(p *AST.Expr, exported bool) {
obj := p.obj;
if exported {
if !obj.IsExported() {
P.Error(obj.pos, `"` + obj.ident + `" should be uppercase`);
}
} else if P.scope_lev == 0 {
if obj.IsExported() {
P.Error(obj.pos, `"` + obj.ident + `" should be lowercase`);
}
}
}
func (P *Parser) VerifyExport(p *AST.Expr, exported bool) {
if !P.naming {
return;
}
for p.tok == Scanner.COMMA {
P.VerifyExport1(p.x, exported);
p = p.y;
}
P.VerifyExport1(p, exported);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// AST support // AST support
...@@ -1510,6 +1538,7 @@ func (P *Parser) ParseConstSpec(exported bool, pos int) *AST.Decl { ...@@ -1510,6 +1538,7 @@ func (P *Parser) ParseConstSpec(exported bool, pos int) *AST.Decl {
} }
P.Declare(d.ident, AST.CONST); P.Declare(d.ident, AST.CONST);
P.VerifyExport(d.ident, exported);
P.Ecart(); P.Ecart();
return d; return d;
...@@ -1524,6 +1553,8 @@ func (P *Parser) ParseTypeSpec(exported bool, pos int) *AST.Decl { ...@@ -1524,6 +1553,8 @@ func (P *Parser) ParseTypeSpec(exported bool, pos int) *AST.Decl {
d.typ = P.ParseType(); d.typ = P.ParseType();
P.opt_semi = true; P.opt_semi = true;
P.VerifyExport(d.ident, exported);
P.Ecart(); P.Ecart();
return d; return d;
} }
...@@ -1546,6 +1577,7 @@ func (P *Parser) ParseVarSpec(exported bool, pos int) *AST.Decl { ...@@ -1546,6 +1577,7 @@ func (P *Parser) ParseVarSpec(exported bool, pos int) *AST.Decl {
} }
P.Declare(d.ident, AST.VAR); P.Declare(d.ident, AST.VAR);
P.VerifyExport(d.ident, exported);
P.Ecart(); P.Ecart();
return d; return d;
...@@ -1630,6 +1662,10 @@ func (P *Parser) ParseFunctionDecl(exported bool) *AST.Decl { ...@@ -1630,6 +1662,10 @@ func (P *Parser) ParseFunctionDecl(exported bool) *AST.Decl {
P.scope_lev--; P.scope_lev--;
} }
if recv == nil || exported {
P.VerifyExport(d.ident, exported);
}
P.Ecart(); P.Ecart();
return d; return d;
} }
......
...@@ -25,6 +25,7 @@ func init() { ...@@ -25,6 +25,7 @@ func init() {
Flag.BoolVar(&flags.columns, "columns", Platform.USER == "gri", "print column info in error messages"); Flag.BoolVar(&flags.columns, "columns", Platform.USER == "gri", "print column info in error messages");
Flag.BoolVar(&flags.testmode, "t", false, "test mode: interprets /* ERROR */ and /* SYNC */ comments"); Flag.BoolVar(&flags.testmode, "t", false, "test mode: interprets /* ERROR */ and /* SYNC */ comments");
Flag.BoolVar(&flags.tokenchan, "token_chan", false, "use token channel for scanner-parser connection"); Flag.BoolVar(&flags.tokenchan, "token_chan", false, "use token channel for scanner-parser connection");
Flag.BoolVar(&flags.naming, "naming", false, "verify export naming scheme");
} }
...@@ -54,7 +55,7 @@ func main() { ...@@ -54,7 +55,7 @@ func main() {
if nerrors > 0 { if nerrors > 0 {
return; return;
} }
if !*silent && !flags.testmode { if !flags.naming && !*silent && !flags.testmode {
Printer.Print(prog); Printer.Print(prog);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment