2 Lexing and Parsing
This library provides facilities for lexing and parsing JavaScript source. It be can required via:
(require (planet dherman/javascript:6/parse)) |
2.1 Input Sources
An input-source is either a string, a path, or an input-port.
x : any |
Determines whether x is an input-source.
in : input-source? |
Produces an input port for reading from in.
2.2 Source Representation
| |||||
offset : exact-positive-integer? | |||||
line : exact-positive-integer? | |||||
col : exact-nonnegative-integer? |
Re-exported from the PLT Scheme collection parser-tools/lex.
| |||||
source : any | |||||
start : position? | |||||
end : position? |
Represents a region of text from start (inclusive) to end (exclusive).
| |||||
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.
| ||||||||
type : symbol? | ||||||||
| ||||||||
location : region? |
A single token of input.
rgn : region? |
Produces a string representation of a region, convenient for debugging and error reporting.
2.3 Syntax Errors
| |||||
source : (implementation?/c lexer<%>) | |||||
location : region? | |||||
text : any |
2.4 Lexer Objects
lexer<%> : interface? |
A JavaScript lexical scanner.
(send a-lexer fail fmt arg ) → any
fmt : string?
arg : any
Raises an exn:fail:syntax exception with error message (format fmt arg ...).
(send a-lexer fail/loc loc text fmt arg ) → any
loc : region?
text : any
fmt : string?
arg : any
Raises an exn:fail:syntax exception with source location loc and error message (format fmt arg ...).
(send a-lexer done?) → boolean?
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.
(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.
(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.
(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.
(send a-lexer peek-token/same-line) → token?
Similar to peek-token, but does not lex past end-of-line sequences.
(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.
(send a-lexer read-token/same-line) → token?
Similar to read-token, but does not lex past end-of-line sequences.
(send a-lexer unread-token) → any
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.
lexer% : class? | ||
superclass: object% | ||
|
An implementation of lexer<%>, a JavaScript lexical scanner.
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
in : input-source |
Convenience function for producing a functional lexer from an input source.
2.6 Parser Objects
parser<%> : interface? |
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 parse-function-expression) → Expression?
Parses a function expression.
(send a-parser skip-empty-tokens) → any
Skips past meaningless whitespace tokens.
parser% : class? | ||
superclass: object% | ||
|
An implementation of parser<%>, a JavaScript 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
in : input-source? |
Parses a JavaScript script from in.
in : input-source? |
Parses a JavaScript expression from in.
in : input-source? |
Parses a JavaScript function expression from in.
in : input-source? |
Parses a JavaScript source element from in.
2.8 S-Expression Syntax
This package also recognizes an alternative S-expression syntax for JavaScript.
x : any |
Determines whether x is an S-expression.
s : sexp? |
Parses an expression.
s : sexp? |
Parses a statement.
s : sexp? |
Parses a source element.