@@ -107,26 +107,59 @@ func (c *cc) convertAttach_stmtContext(n *parser.Attach_stmtContext) ast.Node {
107107}
108108
109109func (c * cc ) convertCreate_table_stmtContext (n * parser.Create_table_stmtContext ) ast.Node {
110+ tokenStream := n .GetParser ().GetTokenStream ().(* antlr.CommonTokenStream )
111+
110112 stmt := & ast.CreateTableStmt {
111113 Name : parseTableName (n ),
112114 IfNotExists : n .EXISTS_ () != nil ,
115+ Comment : comment (tokenStream , n , false ),
113116 }
117+
114118 for _ , idef := range n .AllColumn_def () {
115119 if def , ok := idef .(* parser.Column_defContext ); ok {
116120 typeName := "any"
117121 if def .Type_name () != nil {
118122 typeName = def .Type_name ().GetText ()
119123 }
124+
120125 stmt .Cols = append (stmt .Cols , & ast.ColumnDef {
121126 Colname : identifier (def .Column_name ().GetText ()),
122127 IsNotNull : hasNotNullConstraint (def .AllColumn_constraint ()),
123128 TypeName : & ast.TypeName {Name : typeName },
129+ Comment : comment (tokenStream , def , true ),
124130 })
131+
125132 }
126133 }
127134 return stmt
128135}
129136
137+ // comment returns the comment associated with the given context. The parameter right indicates whether the comment is
138+ // to the right or to the left of the context.
139+ func comment (tokenStream * antlr.CommonTokenStream , ctx antlr.ParserRuleContext , right bool ) string {
140+ var (
141+ hiddenTokens []antlr.Token
142+ comment string
143+ )
144+
145+ if right {
146+ hiddenTokens = tokenStream .GetHiddenTokensToRight (ctx .GetStop ().GetTokenIndex ()+ 1 , antlr .TokenHiddenChannel )
147+ } else {
148+ hiddenTokens = tokenStream .GetHiddenTokensToLeft (ctx .GetStart ().GetTokenIndex (), antlr .TokenHiddenChannel )
149+ }
150+
151+ for _ , token := range hiddenTokens {
152+ // Filter for single-line comments
153+ if token .GetTokenType () == parser .SQLiteLexerSINGLE_LINE_COMMENT {
154+ // Remove "--" and leading/trailing whitespaces
155+ comment = strings .TrimSpace (strings .TrimPrefix (token .GetText (), "--" ))
156+ return comment
157+ }
158+ }
159+
160+ return ""
161+ }
162+
130163func (c * cc ) convertCreate_virtual_table_stmtContext (n * parser.Create_virtual_table_stmtContext ) ast.Node {
131164 switch moduleName := n .Module_name ().GetText (); moduleName {
132165 case "fts5" :
0 commit comments