Version: 4.1.5.5
2 Lexing and Parsing
This library provides facilities for lexing and parsing JavaScript source. It can be
required via:
(require (planet dherman/javascript:9:2/parse)) |
2.1 Input Sources
An input-source is either a string, a path, or an input port.
Determines whether x is an input-source.
(input-source->input-port in) → input-port? |
in : input-source? |
Produces an input port for reading from in.
2.2 Source Representation
Re-exported from the PLT Scheme collection parser-tools/lex.
Represents a region of text from start (inclusive) to end (exclusive).
(struct regexp-contents (pattern global? case-insensitive?)) |
pattern : string |
global? : boolean? |
case-insensitive? : boolean? |
The source to a regular expression token, with regular expression pattern pattern and
flags global?, representing the /g option, and case-insensitive?,
representing the /i option.
(struct token (type contents location)) |
type : symbol? |
|
location : region? |
A single token of input.
(region->string rgn) → string? |
rgn : region? |
Produces a string representation of a region, convenient for debugging and error reporting.
2.3 Syntax Errors
2.4 Lexer Objects
A JavaScript lexical scanner.
Raises an exn:fail:syntax exception with error message (format fmt arg ...).
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.
(send a-lexer current-token) → token? |
Produces the current token in the lexer’s input stream.
(send a-lexer match type) → (optional token?) |
type : symbol? |
Attempts to read a token of type type, producing the token on success and #f on failure.
Attempts to read a token of type type, produce the token on success and raising an exn:fail:syntax exception on failure.
Produces the next token (after skipping skip tokens) in the input stream without changing the current position in the input stream.
Similar to peek-token, but assumes the lexer is being used in a parsing state that expects an infix operator.
(send a-lexer peek-token/same-line) → token? |
Similar to peek-token, but does not lex past end-of-line sequences.
Produces the next token (after skipping skip tokens) in the input stream and updates the current position in the input stream.
(send a-lexer read-token/same-line) → token? |
Similar to read-token, but does not lex past end-of-line sequences.
Rewinds the current position in the input stream by one token.
(send a-lexer skip-whitespace) → any |
Skips past any whitespace in the underlying input port.
An implementation of lexer<%>, a JavaScript lexical scanner.
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
(lex in) → (-> token?) |
in : input-source? |
Convenience function for producing a functional lexer from an input source.
2.6 Parser Objects
A JavaScript parser.
(send a-parser parse-source-element) → SourceElement? |
Parses a single source element.
(send a-parser parse-source-elements) |
→ (listof SourceElement?) |
Parses a sequence of source elements.
(send a-parser parse-expression) → Expression? |
Parses a single expression.
(send a-parser skip-empty-tokens) → any |
Skips past meaningless whitespace tokens.
An implementation of parser<%>, a JavaScript parser.
(new parser% [lexer lexer]) → (is-a?/c parser%) |
lexer : (implementation/c lexer<%>) |
Constructs a new parser% which receives tokens from lexer.
(input-source->parser in) → (is-a?/c parser<%>) |
in : input-source? |
Produces a JavaScript parser for the input from in.
2.7 Parsing Functions
(parse-program-unit in) → (listof SourceElement?) |
in : input-source? |
Parses a JavaScript program unit from in.
(parse-expression in) → Expression? |
in : input-source? |
Parses a JavaScript expression from in.
(parse-function-constructor args body) → FunctionExpression? |
args : string? |
body : string? |
Uses the arguments constructed from the JavaScript Function constructor to parse a function expression.
The args string represents the comma-separated formal parameter list, and the body string
represents the function body (not including the surrounding braces).
(parse-source-element in) → SourceElement? |
in : input-source? |
Parses a JavaScript source element from in.