diff --git a/comment/comment.go b/comment/comment.go new file mode 100644 index 0000000..a75f53b --- /dev/null +++ b/comment/comment.go @@ -0,0 +1,5 @@ +package comment + +type Comment interface { + String() string +} diff --git a/comment/doc_comment.go b/comment/doc_comment.go new file mode 100644 index 0000000..9de49c7 --- /dev/null +++ b/comment/doc_comment.go @@ -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 +} diff --git a/comment/plain_comment.go b/comment/plain_comment.go new file mode 100644 index 0000000..f740ac2 --- /dev/null +++ b/comment/plain_comment.go @@ -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 +} diff --git a/parser/lexer.go b/parser/lexer.go index 7efb4bc..d0f5dc8 100644 --- a/parser/lexer.go +++ b/parser/lexer.go @@ -7,6 +7,7 @@ import ( "unicode" "github.com/cznic/golex/lex" + "github.com/z7zmey/php-parser/comment" ) // Allocate Character classes anywhere in [0x80, 0xFF]. @@ -21,6 +22,7 @@ type lexer struct { stateStack []int lineNumber int phpDocComment string + comments []comment.Comment } func rune2Class(r rune) int { @@ -43,7 +45,7 @@ func newLexer(src io.Reader, fName string) *lexer { if err != nil { panic(err) } - return &lexer{lx, []int{0}, 1, ""} + return &lexer{lx, []int{0}, 1, "", []comment.Comment{}} } func (l *lexer) ungetN(n int) []byte { diff --git a/parser/scanner.go b/parser/scanner.go index f8b8827..350b3c0 100644 --- a/parser/scanner.go +++ b/parser/scanner.go @@ -11,6 +11,7 @@ package parser import ( "bytes" "fmt" + "github.com/z7zmey/php-parser/comment" "github.com/z7zmey/php-parser/token" ) @@ -8263,8 +8264,10 @@ yyrule126: // ([/][*])|([/][*][*]) lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil))) if is_doc_comment { l.phpDocComment = string(l.TokenBytes(nil)) + l.comments = append(l.comments, comment.NewDocComment(string(l.TokenBytes(nil)))) // return T_DOC_COMMENT } else { + l.comments = append(l.comments, comment.NewPlainComment(string(l.TokenBytes(nil)))) // return T_COMMENT } goto yystate0 diff --git a/parser/scanner.l b/parser/scanner.l index 5f7809f..d793ff2 100644 --- a/parser/scanner.l +++ b/parser/scanner.l @@ -11,6 +11,7 @@ import ( "fmt" "bytes" "github.com/z7zmey/php-parser/token" + "github.com/z7zmey/php-parser/comment" ) const ( @@ -271,8 +272,10 @@ NEW_LINE (\r|\n|\r\n) lval.token = token.NewToken(l.handleNewLine(l.TokenBytes(nil))) if is_doc_comment { l.phpDocComment = string(l.TokenBytes(nil)) + l.comments = append(l.comments, comment.NewDocComment(string(l.TokenBytes(nil)))) // return T_DOC_COMMENT } else { + l.comments = append(l.comments, comment.NewPlainComment(string(l.TokenBytes(nil)))) // return T_COMMENT }