add comment struct

This commit is contained in:
z7zmey 2018-01-03 00:12:28 +02:00
parent bbf9b0cc09
commit e8655c5f4c
6 changed files with 44 additions and 1 deletions

5
comment/comment.go Normal file
View File

@ -0,0 +1,5 @@
package comment
type Comment interface {
String() string
}

15
comment/doc_comment.go Normal file
View File

@ -0,0 +1,15 @@
package comment
type DocComment struct {
value string
}
func NewDocComment(value string) Comment {
return &DocComment{
value,
}
}
func (c *DocComment) String() string {
return c.value
}

15
comment/plain_comment.go Normal file
View File

@ -0,0 +1,15 @@
package comment
type PlainComment struct {
value string
}
func NewPlainComment(value string) Comment {
return &PlainComment{
value,
}
}
func (c *PlainComment) String() string {
return c.value
}

View File

@ -7,6 +7,7 @@ import (
"unicode" "unicode"
"github.com/cznic/golex/lex" "github.com/cznic/golex/lex"
"github.com/z7zmey/php-parser/comment"
) )
// Allocate Character classes anywhere in [0x80, 0xFF]. // Allocate Character classes anywhere in [0x80, 0xFF].
@ -21,6 +22,7 @@ type lexer struct {
stateStack []int stateStack []int
lineNumber int lineNumber int
phpDocComment string phpDocComment string
comments []comment.Comment
} }
func rune2Class(r rune) int { func rune2Class(r rune) int {
@ -43,7 +45,7 @@ func newLexer(src io.Reader, fName string) *lexer {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return &lexer{lx, []int{0}, 1, ""} return &lexer{lx, []int{0}, 1, "", []comment.Comment{}}
} }
func (l *lexer) ungetN(n int) []byte { func (l *lexer) ungetN(n int) []byte {

View File

@ -11,6 +11,7 @@ package parser
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/z7zmey/php-parser/comment"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/token"
) )
@ -8263,8 +8264,10 @@ yyrule126: // ([/][*])|([/][*][*])
lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil))) lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil)))
if is_doc_comment { if is_doc_comment {
l.phpDocComment = string(l.TokenBytes(nil)) l.phpDocComment = string(l.TokenBytes(nil))
l.comments = append(l.comments, comment.NewDocComment(string(l.TokenBytes(nil))))
// return T_DOC_COMMENT // return T_DOC_COMMENT
} else { } else {
l.comments = append(l.comments, comment.NewPlainComment(string(l.TokenBytes(nil))))
// return T_COMMENT // return T_COMMENT
} }
goto yystate0 goto yystate0

View File

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"bytes" "bytes"
"github.com/z7zmey/php-parser/token" "github.com/z7zmey/php-parser/token"
"github.com/z7zmey/php-parser/comment"
) )
const ( const (
@ -271,8 +272,10 @@ NEW_LINE (\r|\n|\r\n)
lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil))) lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil)))
if is_doc_comment { if is_doc_comment {
l.phpDocComment = string(l.TokenBytes(nil)) l.phpDocComment = string(l.TokenBytes(nil))
l.comments = append(l.comments, comment.NewDocComment(string(l.TokenBytes(nil))))
// return T_DOC_COMMENT // return T_DOC_COMMENT
} else { } else {
l.comments = append(l.comments, comment.NewPlainComment(string(l.TokenBytes(nil))))
// return T_COMMENT // return T_COMMENT
} }