2 Lexing and Parsing
This library provides facilities for lexing and parsing JavaScript source. It can be required via:
(require javascript/parse) | package: javascript |
2.1 Input Sources
An input-source is either a string, a path, or an input port.
procedure
(input-source? x) → boolean?
x : any
procedure
in : input-source?
2.2 Source Representation
struct
(struct position (offset line col) #:extra-constructor-name make-position) offset : exact-positive-integer? line : exact-positive-integer? col : exact-nonnegative-integer?
struct
(struct region (source start end) #:extra-constructor-name make-region) source : any start : position? end : position?
struct
(struct regexp-contents (pattern global? case-insensitive?) #:extra-constructor-name make-regexp-contents) pattern : string global? : boolean? case-insensitive? : boolean?
struct
(struct token (type contents location) #:extra-constructor-name make-token) type : symbol?
contents :
(optional/c (or/c string? number? symbol? regexp-contents?)) location : region?
procedure
(region->string rgn) → string?
rgn : region?
2.3 Syntax Errors
struct
(struct exn:fail:syntax exn:fail (source location text) #:extra-constructor-name make-exn:fail:syntax) source : (implementation?/c lexer<%>) location : region? text : any
2.4 Lexer Objects
|
Raises an exn:fail:syntax exception with source location loc and error message (format fmt arg ...).Determines whether the end of input has been reached.
method
(send a-lexer current-token) → token?
Produces the current token in the lexer’s input stream.Attempts to read a token of type type, producing the token on success and #f on failure.
method
(send a-lexer must-match type) → token?
type : symbol? Attempts to read a token of type type, produce the token on success and raising an exn:fail:syntax exception on failure.
method
(send a-lexer peek-token [skip]) → token?
skip : natural-number/c = 0 Produces the next token (after skipping skip tokens) in the input stream without changing the current position in the input stream.
method
(send a-lexer peek-token/infix-operator [skip]) → token?
skip : natural-number/c = 0 Similar to peek-token, but assumes the lexer is being used in a parsing state that expects an infix operator.
method
(send a-lexer peek-token/same-line) → token?
Similar to peek-token, but does not lex past end-of-line sequences.
method
(send a-lexer read-token [skip]) → token?
skip : natural-number/c = 0 Produces the next token (after skipping skip tokens) in the input stream and updates the current position in the input stream.
method
(send a-lexer read-token/same-line) → token?
Similar to read-token, but does not lex past end-of-line sequences.
method
(send a-lexer unread-token) → any
Rewinds the current position in the input stream by one token.
method
(send a-lexer skip-whitespace) → any
Skips past any whitespace in the underlying input port.
constructor
port : input-port? name : any = (object-name port) Constructs a new lexer% which reads tokens from port. The optional name argument is used for source location information and error reporting.
2.5 Lexing Functions
procedure
in : input-source?
2.6 Parser Objects
|
method
(send a-parser parse-source-element) → SourceElement?
Parses a single source element.
method
(send a-parser parse-source-elements)
→ (listof SourceElement?) Parses a sequence of source elements.
method
(send a-parser parse-expression) → Expression?
Parses a single expression.
method
(send a-parser skip-empty-tokens) → any
Skips past meaningless whitespace tokens.
Constructs a new parser% which receives tokens from lexer.
procedure
(input-source->parser in) → (is-a?/c parser<%>)
in : input-source?
2.7 Parsing Functions
procedure
(parse-program-unit in) → (listof SourceElement?)
in : input-source?
procedure
(parse-expression in) → Expression?
in : input-source?
procedure
(parse-function-constructor args body) → FunctionExpression?
args : string? body : string?
procedure
in : input-source?