On this page:
2.1 Input Sources
input-source?
input-source->input-port
2.2 Source Representation
position
region
regexp-contents
token
region->string
2.3 Syntax Errors
exn:  fail:  syntax
2.4 Lexer Objects
lexer<%>
fail
fail/  loc
done?
current-token
match
must-match
peek-token
peek-token/  infix-operator
peek-token/  same-line
read-token
read-token/  same-line
unread-token
skip-whitespace
lexer%
new
2.5 Lexing Functions
lex
2.6 Parser Objects
parser<%>
parse-source-element
parse-source-elements
parse-expression
skip-empty-tokens
parser%
new
input-source->parser
2.7 Parsing Functions
parse-program-unit
parse-expression
parse-function-constructor
parse-source-element

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
Determines whether x is an input-source.

procedure

(input-source->input-port in)  input-port?

  in : input-source?
Produces an input port for reading from in.

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?
Re-exported from the PLT Scheme collection parser-tools/lex.

struct

(struct region (source start end)
    #:extra-constructor-name make-region)
  source : any
  start : position?
  end : position?
Represents a region of text from start (inclusive) to end (exclusive).

struct

(struct regexp-contents (pattern global? case-insensitive?)
    #:extra-constructor-name make-regexp-contents)
  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

(struct token (type contents location)
    #:extra-constructor-name make-token)
  type : symbol?
  contents : 
(optional/c
 (or/c string? number? symbol? regexp-contents?))
  location : region?
A single token of input.

procedure

(region->string rgn)  string?

  rgn : region?
Produces a string representation of a region, convenient for debugging and error reporting.

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🔗ℹ

interface

lexer<%> : interface?

A JavaScript lexical scanner.

method

(send a-lexer fail fmt arg ...)  any

  fmt : string?
  arg : any
Raises an exn:fail:syntax exception with error message (format fmt arg ...).

method

(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 ...).

method

(send a-lexer done?)  boolean?

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.

method

(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.

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.

class

lexer% : class?

  superclass: object%

  extends: lexer<%>
An implementation of lexer<%>, a JavaScript lexical scanner.

constructor

(new lexer% [port port] [[name name]])  (is-a?/c lexer%)

  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

(lex in)  (-> token?)

  in : input-source?
Convenience function for producing a functional lexer from an input source.

2.6 Parser Objects🔗ℹ

interface

parser<%> : interface?

A JavaScript parser.

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.

class

parser% : class?

  superclass: object%

  extends: parser<%>
An implementation of parser<%>, a JavaScript parser.

constructor

(new parser% [lexer lexer])  (is-a?/c parser%)

  lexer : (implementation/c lexer<%>)
Constructs a new parser% which receives tokens from lexer.

procedure

(input-source->parser in)  (is-a?/c parser<%>)

  in : input-source?
Produces a JavaScript parser for the input from in.

2.7 Parsing Functions🔗ℹ

procedure

(parse-program-unit in)  (listof SourceElement?)

  in : input-source?
Parses a JavaScript program unit from in.

procedure

(parse-expression in)  Expression?

  in : input-source?
Parses a JavaScript expression from in.

procedure

(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).

procedure

(parse-source-element in)  SourceElement?

  in : input-source?
Parses a JavaScript source element from in.