Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
22e0e1b0
Commit
22e0e1b0
authored
Nov 14, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- better comment classification
- related cleanups R=r OCL=19227 CL=19227
parent
3c2f0ae1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
58 deletions
+68
-58
ast.go
usr/gri/pretty/ast.go
+3
-3
parser.go
usr/gri/pretty/parser.go
+9
-2
printer.go
usr/gri/pretty/printer.go
+16
-25
scanner.go
usr/gri/pretty/scanner.go
+40
-28
No files found.
usr/gri/pretty/ast.go
View file @
22e0e1b0
...
@@ -258,14 +258,14 @@ export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false);
...
@@ -258,14 +258,14 @@ export var BadDecl = NewDecl(0, Scanner.ILLEGAL, false);
// Program
// Program
export
type
Comment
struct
{
export
type
Comment
struct
{
pos
int
;
pos
,
tok
int
;
text
string
;
text
string
;
}
}
export
func
NewComment
(
pos
int
,
text
string
)
*
Comment
{
export
func
NewComment
(
pos
,
tok
int
,
text
string
)
*
Comment
{
c
:=
new
(
Comment
);
c
:=
new
(
Comment
);
c
.
pos
,
c
.
t
ext
=
pos
,
text
;
c
.
pos
,
c
.
t
ok
,
c
.
text
=
pos
,
tok
,
text
;
return
c
;
return
c
;
}
}
...
...
usr/gri/pretty/parser.go
View file @
22e0e1b0
...
@@ -77,8 +77,15 @@ func (P *Parser) Next0() {
...
@@ -77,8 +77,15 @@ func (P *Parser) Next0() {
func
(
P
*
Parser
)
Next
()
{
func
(
P
*
Parser
)
Next
()
{
for
P
.
Next0
();
P
.
tok
==
Scanner
.
COMMENT
;
P
.
Next0
()
{
// TODO This is too expensive for every token - fix
P
.
comments
.
Add
(
AST
.
NewComment
(
P
.
pos
,
P
.
val
));
for
P
.
Next0
();
P
.
tok
==
Scanner
.
COMMENT_WW
||
P
.
tok
==
Scanner
.
COMMENT_WB
||
P
.
tok
==
Scanner
.
COMMENT_BW
||
P
.
tok
==
Scanner
.
COMMENT_BB
;
P
.
Next0
()
{
P
.
comments
.
Add
(
AST
.
NewComment
(
P
.
pos
,
P
.
tok
,
P
.
val
));
}
}
}
}
...
...
usr/gri/pretty/printer.go
View file @
22e0e1b0
...
@@ -234,45 +234,36 @@ func (P *Printer) String(pos int, s string) {
...
@@ -234,45 +234,36 @@ func (P *Printer) String(pos int, s string) {
assert
(
len
(
text
)
>=
3
);
// classification char + "//" or "/*"
assert
(
len
(
text
)
>=
3
);
// classification char + "//" or "/*"
// classify comment
// classify comment
switch
text
[
0
]
{
switch
comment
.
tok
{
case
' '
:
case
Scanner
.
COMMENT_BB
:
// not only white space before comment on the same line
// black space before and after comment on the same line
// - put into next cell if //-style comment
// - print surrounded by blanks
// - preceed with a space if /*-style comment
P
.
buf
.
Print
(
" "
);
//print("[case a][", text[1 : len(text)], "]");
P
.
buf
.
Print
(
text
);
if
text
[
2
]
==
'/'
{
P
.
buf
.
Print
(
" "
);
P
.
buf
.
Tab
();
}
else
{
case
Scanner
.
COMMENT_BW
:
P
.
buf
.
Print
(
" "
);
// only white space after comment on the same line
}
// - put into next cell
P
.
buf
.
Tab
();
/*
P
.
buf
.
Print
(
text
);
case '\n':
// comment starts at beginning of line
// - reproduce exactly
//print("[case b][", text[1 : len(text)], "]");
if !P.buf.AtLineBegin() {
P.buf.Newline();
}
*/
case
'\n'
,
'\t'
:
case
Scanner
.
COMMENT_WW
,
Scanner
.
COMMENT_WB
:
// only white space before comment on the same line
// only white space before comment on the same line
// - indent
// - indent
//print("[case c][", text[1 : len(text)], "]");
if
!
P
.
buf
.
EmptyLine
()
{
if
!
P
.
buf
.
EmptyLine
()
{
P
.
buf
.
Newline
();
P
.
buf
.
Newline
();
}
}
for
i
:=
P
.
indent
;
i
>
0
;
i
--
{
for
i
:=
P
.
indent
;
i
>
0
;
i
--
{
P
.
buf
.
Tab
();
P
.
buf
.
Tab
();
}
}
P
.
buf
.
Print
(
text
);
default
:
default
:
panic
(
"UNREACHABLE"
);
panic
(
"UNREACHABLE"
);
}
}
P
.
buf
.
Print
(
text
[
1
:
len
(
text
)]);
if
text
[
1
]
==
'/'
{
if
text
[
2
]
==
'/'
{
// line comments must end in newline
// line comments must end in newline
// TODO should we set P.newl instead?
// TODO should we set P.newl instead?
P
.
buf
.
Newline
();
P
.
buf
.
Newline
();
...
...
usr/gri/pretty/scanner.go
View file @
22e0e1b0
...
@@ -13,9 +13,13 @@ export const (
...
@@ -13,9 +13,13 @@ export const (
INT
;
INT
;
FLOAT
;
FLOAT
;
STRING
;
STRING
;
COMMENT
;
EOF
;
EOF
;
COMMENT_BB
;
COMMENT_BW
;
COMMENT_WB
;
COMMENT_WW
;
ADD
;
ADD
;
SUB
;
SUB
;
MUL
;
MUL
;
...
@@ -116,9 +120,13 @@ export func TokenString(tok int) string {
...
@@ -116,9 +120,13 @@ export func TokenString(tok int) string {
case
INT
:
return
"INT"
;
case
INT
:
return
"INT"
;
case
FLOAT
:
return
"FLOAT"
;
case
FLOAT
:
return
"FLOAT"
;
case
STRING
:
return
"STRING"
;
case
STRING
:
return
"STRING"
;
case
COMMENT
:
return
"COMMENT"
;
case
EOF
:
return
"EOF"
;
case
EOF
:
return
"EOF"
;
case
COMMENT_BB
:
return
"COMMENT_BB"
;
case
COMMENT_BW
:
return
"COMMENT_BW"
;
case
COMMENT_WB
:
return
"COMMENT_WB"
;
case
COMMENT_WW
:
return
"COMMENT_WW"
;
case
ADD
:
return
"+"
;
case
ADD
:
return
"+"
;
case
SUB
:
return
"-"
;
case
SUB
:
return
"-"
;
case
MUL
:
return
"*"
;
case
MUL
:
return
"*"
;
...
@@ -518,29 +526,23 @@ func (S *Scanner) Expect(ch int) {
...
@@ -518,29 +526,23 @@ func (S *Scanner) Expect(ch int) {
}
}
func
(
S
*
Scanner
)
SkipWhitespace
()
int
{
// Returns true if a newline was seen, returns false otherwise.
pos
:=
-
1
;
// no new line position yet
func
(
S
*
Scanner
)
SkipWhitespace
()
bool
{
sawnl
:=
S
.
chpos
==
0
;
// file beginning is always start of a new line
if
S
.
chpos
==
0
{
// file beginning is always start of a new line
pos
=
0
;
}
for
{
for
{
switch
S
.
ch
{
switch
S
.
ch
{
case
'\t'
,
'\r'
,
' '
:
// nothing to do
case
'\t'
,
'\r'
,
' '
:
// nothing to do
case
'\n'
:
pos
=
S
.
pos
;
// remember start of new line
case
'\n'
:
sawnl
=
true
;
default
:
goto
exit
;
default
:
return
sawnl
;
}
}
S
.
Next
();
S
.
Next
();
}
}
panic
(
"UNREACHABLE"
);
exit
:
return
false
;
return
pos
;
}
}
func
(
S
*
Scanner
)
ScanComment
(
nlpos
int
)
string
{
func
(
S
*
Scanner
)
ScanComment
(
leading_ws
bool
)
(
tok
int
,
val
string
)
{
// first '/' already consumed
// first '/' already consumed
pos
:=
S
.
chpos
-
1
;
pos
:=
S
.
chpos
-
1
;
...
@@ -575,6 +577,12 @@ func (S *Scanner) ScanComment(nlpos int) string {
...
@@ -575,6 +577,12 @@ func (S *Scanner) ScanComment(nlpos int) string {
exit
:
exit
:
comment
:=
S
.
src
[
pos
:
S
.
chpos
];
comment
:=
S
.
src
[
pos
:
S
.
chpos
];
// skip whitespace but stop at line end
for
S
.
ch
==
'\t'
||
S
.
ch
==
'\r'
||
S
.
ch
==
' '
{
S
.
Next
();
}
trailing_ws
:=
S
.
ch
==
'\n'
;
if
S
.
testmode
{
if
S
.
testmode
{
// interpret ERROR and SYNC comments
// interpret ERROR and SYNC comments
oldpos
:=
-
1
;
oldpos
:=
-
1
;
...
@@ -595,18 +603,22 @@ exit:
...
@@ -595,18 +603,22 @@ exit:
S
.
ErrorMsg
(
oldpos
,
"ERROR not found"
);
S
.
ErrorMsg
(
oldpos
,
"ERROR not found"
);
}
}
}
}
if
nlpos
<
0
{
if
leading_ws
{
// not only whitespace before comment on this line
if
trailing_ws
{
comment
=
" "
+
comment
;
tok
=
COMMENT_WW
;
}
else
if
nlpos
==
pos
{
}
else
{
// comment starts at the beginning of the line
tok
=
COMMENT_WB
;
comment
=
"
\n
"
+
comment
;
}
}
else
{
}
else
{
// only whitespace before comment on this line
if
trailing_ws
{
comment
=
"
\t
"
+
comment
;
tok
=
COMMENT_BW
;
}
else
{
tok
=
COMMENT_BB
;
}
}
}
return
comment
;
return
tok
,
comment
;
}
}
...
@@ -835,7 +847,7 @@ func (S *Scanner) Select4(tok0, tok1, ch2, tok2, tok3 int) int {
...
@@ -835,7 +847,7 @@ func (S *Scanner) Select4(tok0, tok1, ch2, tok2, tok3 int) int {
func
(
S
*
Scanner
)
Scan
()
(
pos
,
tok
int
,
val
string
)
{
func
(
S
*
Scanner
)
Scan
()
(
pos
,
tok
int
,
val
string
)
{
nlpos
:=
S
.
SkipWhitespace
();
sawnl
:=
S
.
SkipWhitespace
();
pos
,
tok
=
S
.
chpos
,
ILLEGAL
;
pos
,
tok
=
S
.
chpos
,
ILLEGAL
;
...
@@ -875,7 +887,7 @@ func (S *Scanner) Scan() (pos, tok int, val string) {
...
@@ -875,7 +887,7 @@ func (S *Scanner) Scan() (pos, tok int, val string) {
case
'*'
:
tok
=
S
.
Select2
(
MUL
,
MUL_ASSIGN
);
case
'*'
:
tok
=
S
.
Select2
(
MUL
,
MUL_ASSIGN
);
case
'/'
:
case
'/'
:
if
S
.
ch
==
'/'
||
S
.
ch
==
'*'
{
if
S
.
ch
==
'/'
||
S
.
ch
==
'*'
{
tok
,
val
=
COMMENT
,
S
.
ScanComment
(
nlpos
);
tok
,
val
=
S
.
ScanComment
(
sawnl
);
}
else
{
}
else
{
tok
=
S
.
Select2
(
QUO
,
QUO_ASSIGN
);
tok
=
S
.
Select2
(
QUO
,
QUO_ASSIGN
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment