AlexKnauth / syntax-sloc

counts the number of source lines of code in a racket syntax object
MIT License
3 stars 1 forks source link

Count lines of contract, lines of type, ... #4

Open bennn opened 8 years ago

bennn commented 8 years ago

Right now we only count lines of code. We should also be able to count lines of contracts, lines of type annotations, and other things.

Proposal:

AlexKnauth commented 8 years ago

Something like this?

(define (type-ann-syntax? stx)
  (syntax-parse stx #:datum-literals (:)
    [(: . _) #true]
    [_ #false]))

(syntax-sloc (read-lang-file ....) #:count-syntax? type-ann-syntax?)

The issue is that the result of read-lang-file, although it's a syntax object, doesn't have any scopes or bindings associated with it, so it can't do any better that a #:datum-literals (:) match. If you could expand the syntax first?

(require (only-in typed/racket/base :))
;; type-ann-syntax-from? : Any -> (Expanded-Stx -> Boolean)
(define ((type-ann-syntax-from? src) stx)
  (and
   (equal? (syntax-source stx) src)
   (syntax-parse stx #:literals (:)
     [(: . _) #true]
     [_ #false])))

(syntax-sloc
 (local-expand
  (read-lang-file ....)
  'top-level
  (list))
 #:count-syntax? (type-ann-syntax-from? ...))

Something like that?

bennn commented 8 years ago

(Dear Ben use https://github.com/AlexKnauth/syntax-sloc/commit/e39fdbbf3408e526a031c8a68b9ced99f8db222b to make a pull request)

AlexKnauth commented 8 years ago

See https://gist.github.com/AlexKnauth/71e7b7953ecfa1e002a6ffd05e55b3cc It's draft of a module->provided-identifiers function that handles identifiers that are renamed when imported.

bennn commented 8 years ago

Here's notes on re-using check-syntax to count identifiers from racket/contract. Overall the approach is no good because it misses identifiers defined outside racket/contract but re-provided. Anyway:

  1. Use check-syntax's make-traversal and syncheck:add-jump-to-definition to collect a set of identifiers that jump to an identifier defined in the racket/contract collection. src
  2. Added optional (Listof Bytes) argument to lang-file-sloc. The bytes specify a path relative to the collects folder, as returned by path->collects-relative. src
  3. Note: the update to lang-file-sloc doesn't use syntax-sloc.rkt at all. src
bennn commented 8 years ago

(BTW -- our current goal is just counting the line numbers of identifiers used from the racket/contract module. This is different from the number of contract annotations.)