com.google.caja.plugin.stages
Class Splitter

java.lang.Object
  extended by com.google.caja.plugin.stages.Splitter

final class Splitter
extends java.lang.Object

Splits a sequence of string literals containing ${...} and $name into expressions, by looking for that pattern in the string literals and parsing the content of substitutions as javascript.

The output contains alternating literal and substitutions, and is careful to preserve file positions into the original source.

This code takes a list of string literals and keeps track of positions as (stringLiteralIndex, characterIndexInUndecodedLiteral) pairs.


Nested Class Summary
private static class Splitter.State
           
 
Field Summary
(package private)  int i
          Indices into the literal list, and the current literal.
(package private)  int j
          Indices into the literal list, and the current literal.
(package private)  java.util.List<StringLiteral> literals
          The string literals being processed.
(package private)  MessageQueue mq
          A queue to which parse errors are written.
(package private)  java.util.List<Expression> parts
          Output array.
(package private)  int startOffset
           
(package private)  int startString
          Cursor that is moved from left to right as we process the string literals.
(package private)  Splitter.State state
          Used to keep track of whether we're inside a substitution or not.
 
Constructor Summary
Splitter(java.util.List<StringLiteral> literals, MessageQueue mq)
           
 
Method Summary
(package private) static FilePosition clippedPos(FilePosition p, int start, int end)
          Interpolate the position of a substring of a StringLiteral.
private  void finishBlock(int delta)
          Parse a substitution expression and push it onto the output list.
private  void finishLiteral(int delta)
          Push a literal part onto the output list.
private  void finishReference(int delta)
          Push a reference onto the output list.
private  void mark(int delta)
          Track the last position processed.
(package private)  void split()
          Walk the literal list and generate the output parts list.
private  java.util.List<Pair<java.lang.String,FilePosition>> upTo(int delta)
          Compute the pieces of the string literals that span from the mark to the given delta.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startString

int startString
Cursor that is moved from left to right as we process the string literals.


startOffset

int startOffset

literals

java.util.List<StringLiteral> literals
The string literals being processed. If we're dealing with eval(Template("Foo $bar" + " baz")) then the two string literals will be "Foo $bar" and " baz".


parts

java.util.List<Expression> parts
Output array. Alternating literals (StringLiteral) and substitutions (arbitrary Expression). For eval(Template("Foo $bar Baz")) this is [StringLiteral('Foo '), Reference("bar"), StringLiteral(' Baz')].

Note that not all StringLiterals are literals. In eval(Template("Foo ${'bar'} Baz")), the sole substitution is a StringLiteral.


state

Splitter.State state
Used to keep track of whether we're inside a substitution or not.


i

int i
Indices into the literal list, and the current literal.


j

int j
Indices into the literal list, and the current literal.


mq

MessageQueue mq
A queue to which parse errors are written.

Constructor Detail

Splitter

Splitter(java.util.List<StringLiteral> literals,
         MessageQueue mq)
Method Detail

split

void split()
Walk the literal list and generate the output parts list.


mark

private void mark(int delta)
Track the last position processed.

Parameters:
delta - number of characters from the current position in the string literals.

finishLiteral

private void finishLiteral(int delta)
Push a literal part onto the output list.

Parameters:
delta - of the end of the literal relative to the cursor.

finishReference

private void finishReference(int delta)
Push a reference onto the output list.

Parameters:
delta - of the end of the reference to the cursor.

finishBlock

private void finishBlock(int delta)
Parse a substitution expression and push it onto the output list.

Parameters:
delta - of the end of the block to the cursor.

upTo

private java.util.List<Pair<java.lang.String,FilePosition>> upTo(int delta)
Compute the pieces of the string literals that span from the mark to the given delta.


clippedPos

static FilePosition clippedPos(FilePosition p,
                               int start,
                               int end)
Interpolate the position of a substring of a StringLiteral.



Copyright (C) 2008 Google Inc.
Licensed under the Apache License, Version 2.0