Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a PoC for syntax extension that injects line number #308

Closed
1 of 2 tasks
jcubic opened this issue Feb 9, 2024 · 3 comments
Closed
1 of 2 tasks

Create a PoC for syntax extension that injects line number #308

jcubic opened this issue Feb 9, 2024 · 3 comments
Labels
enhancement New feature or request experiment

Comments

@jcubic
Copy link
Owner

jcubic commented Feb 9, 2024

It would be cool if that would be possible. It will only work when the source code is the text file and not the binary.

The Lexer would need to be modified to count new lines.

Think about the possibility of adding a function that will return line number.

  • Working Example
  • Update documentation
@jcubic jcubic added the enhancement New feature or request label Feb 9, 2024
@jcubic
Copy link
Owner Author

jcubic commented Mar 25, 2024

The Lexer already have metadata information like number of columns or line number. There is only a need to expose this to the user. This will also allow adding line number to stack trace.

@jcubic
Copy link
Owner Author

jcubic commented Mar 25, 2024

This a working example:

(set-special! "#:num" 'line-num lips.specials.SYMBOL)

(define (line-num)
  (let ((token (lips.__parser__.__lexer__.peek true)))
    (+ token.line 1)))

(print #:num) ;; ==> 7
(print #:num) ;; ==> 8

jcubic added a commit that referenced this issue Mar 25, 2024
@jcubic
Copy link
Owner Author

jcubic commented Mar 25, 2024

With this, there will be a way to add full string interpolation #321

This is example dumb raw string syntax:

(set-special! "#raw" 'frob lips.specials.SYMBOL)

(define (frob)
  (let ((lexer lips.__parser__.__lexer__))
    (if (char=? (lexer.peek_char) #\`)
        (begin
          (lexer.skip_char)
          (let loop ((result (vector)) (char (lexer.peek_char)))
            (lexer.skip_char)
            (if (char=? char #\`)
                (result.join "")
                (loop (result.concat char) (lexer.peek_char))))))))

(print #raw`foo \ bar`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request experiment
Projects
None yet
Development

No branches or pull requests

1 participant