@@ -45,6 +45,35 @@ impl Lexer {
4545 self . peek_token = None ;
4646 }
4747
48+ fn skip_whitespace ( & mut self ) {
49+ while self . peek_char ( ) . map_or ( false , u8:: is_ascii_whitespace) {
50+ self . pos += 1 ;
51+ }
52+ }
53+
54+ fn skip_extras ( & mut self ) -> Result < ( ) , Error > {
55+ self . skip_whitespace ( ) ;
56+
57+ // skip over comments
58+ if self . peek_char ( ) . map_or ( false , |c| c == & b'/' ) {
59+ self . next_char ( ) ;
60+ if self . peek_char ( ) . map_or ( false , |c| c == & b'/' ) {
61+ while self . peek_char ( ) . map_or ( false , |c| c != & b'\n' ) {
62+ self . pos += 1 ;
63+ }
64+ self . skip_whitespace ( ) ;
65+ // recurse to skip any other comments:
66+ if self . peek_char ( ) . map_or ( false , |c| c == & b'/' ) {
67+ return self . skip_extras ( ) ;
68+ }
69+ } else {
70+ return Err ( Error :: new ( "Expected '/'" , self . pos - 1 ) ) ;
71+ }
72+ }
73+
74+ Ok ( ( ) )
75+ }
76+
4877 pub ( crate ) fn peek_tok ( & mut self ) -> Result < Option < & Token > , Error > {
4978 if self . peek_token . is_none ( ) {
5079 self . peek_token = self . next_tok ( ) ?;
@@ -57,10 +86,7 @@ impl Lexer {
5786 return Ok ( Some ( peeked) ) ;
5887 }
5988
60- // skip over whitespace
61- while self . peek_char ( ) . map_or ( false , u8:: is_ascii_whitespace) {
62- self . pos += 1 ;
63- }
89+ self . skip_extras ( ) ?;
6490
6591 let mut return_val: Result < Option < Token > , Error > = Ok ( None ) ;
6692
@@ -281,32 +307,57 @@ mod tests {
281307 assert_eq ! ( lexer. next_tok( ) , Ok ( None ) ) ;
282308 }
283309
310+ #[ test]
311+ fn comments ( ) {
312+ expect_tokens (
313+ r#"
314+ // this is a comment
315+ foo
316+ // another comment
317+ "bar"
318+ "# ,
319+ & [
320+ TokenKind :: Identifier ( "foo" . to_string ( ) ) ,
321+ TokenKind :: StringLiteral ( "bar" . to_string ( ) ) ,
322+ ] ,
323+ ) ;
324+ }
325+
284326 #[ test]
285327 fn all_syntax ( ) {
286328 expect_tokens (
287329 r#"
330+ // comment
288331 name: "GetTodos"
289332 description: "Get all todos for a user"
290- authed: true
333+ authed: true // here's another comment
291334
292335 ---
293336
337+ // this is the input (nice no?)
294338 @input { user_id: String }
295339
296340 @output [Todo]
297341
298342 ---
299343
344+ @someAnnotation
300345 Todo {
301346 name: String
302347 due: Date?
348+ // refers to custom enum
303349 priority: Priority?
304350 subtasks: [Todo]
305351 }
306352
353+ // we add this because this is actually
354+ // a postgres enum
355+ // these comments can be many lines long btw
356+ // just double checking that it works
357+ @sqlxType
307358 Priority (
308359 low
309- medium
360+ medium // for the bourgeois case
310361 high
311362 other(String)
312363 )
@@ -335,6 +386,8 @@ mod tests {
335386 TokenKind :: Identifier ( "Todo" . to_string ( ) ) ,
336387 TokenKind :: RightBracket ,
337388 TokenKind :: Rule ,
389+ TokenKind :: At ,
390+ TokenKind :: Identifier ( "someAnnotation" . to_string ( ) ) ,
338391 TokenKind :: Identifier ( "Todo" . to_string ( ) ) ,
339392 TokenKind :: LeftBrace ,
340393 TokenKind :: Identifier ( "name" . to_string ( ) ) ,
@@ -354,6 +407,8 @@ mod tests {
354407 TokenKind :: Identifier ( "Todo" . to_string ( ) ) ,
355408 TokenKind :: RightBracket ,
356409 TokenKind :: RightBrace ,
410+ TokenKind :: At ,
411+ TokenKind :: Identifier ( "sqlxType" . to_string ( ) ) ,
357412 TokenKind :: Identifier ( "Priority" . to_string ( ) ) ,
358413 TokenKind :: LeftParen ,
359414 TokenKind :: Identifier ( "low" . to_string ( ) ) ,
0 commit comments