Skip to content

Commit

Permalink
First draft of Scanning.
Browse files Browse the repository at this point in the history
Also:

- Tweak some styles.
- Allow markers in code to line up asides.
  • Loading branch information
munificent committed Jan 2, 2017
1 parent f9ae7c2 commit 127eed2
Show file tree
Hide file tree
Showing 14 changed files with 1,289 additions and 388 deletions.
6 changes: 5 additions & 1 deletion asset/sass/chapter.scss
Expand Up @@ -99,9 +99,13 @@ article.chapter {
.design-note {
background: hsl(80, 30%, 96%);

code, pre {
code, .codehilite {
background: hsl(80, 20%, 93%);
}

.codehilite {
margin: -12px 0 -12px -12px;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion asset/style.scss
Expand Up @@ -357,7 +357,7 @@ aside {
padding: 1px 2px;
}

pre {
.codehilite {
padding: 6px;
margin: -12px 0;
}
Expand Down
783 changes: 426 additions & 357 deletions book/scanning.md

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions java/com/craftinginterpreters/lox/Lox.java
Expand Up @@ -27,7 +27,7 @@ public static void main(String[] args) throws IOException {
} else if (args.length == 1) {
runFile(args[0]);
} else {
repl();
runPrompt();
}
}
//> run-file
Expand All @@ -44,17 +44,23 @@ private static void runFile(String path) throws IOException {
//< Evaluating Expressions not-yet
}
//< run-file
//> repl
private static void repl() throws IOException {
//> prompt
private static void runPrompt() throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);

for (;;) {
for (;;) { // [repl]
System.out.print("> ");
run(reader.readLine());
//> reset-had-error
hadError = false;
//< reset-had-error
//> Evaluating Expressions not-yet
hadRuntimeError = false;
//< Evaluating Expressions not-yet
}
}
//< repl
//< prompt
//> run
private static void run(String source) {
Scanner scanner = new Scanner(source);
Expand Down
18 changes: 6 additions & 12 deletions java/com/craftinginterpreters/lox/Scanner.java
Expand Up @@ -8,7 +8,7 @@

import static com.craftinginterpreters.lox.TokenType.*;

class Scanner {
class Scanner { // [files]
//> keyword-map
private static final Map<String, TokenType> keywords;

Expand Down Expand Up @@ -78,7 +78,8 @@ private void scanToken() {
//> slash
case '/':
if (match('/')) {
comment();
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd()) advance();
} else {
addToken(SLASH);
}
Expand Down Expand Up @@ -122,12 +123,6 @@ private void scanToken() {
}
}
//< scan-token
//> comment
private void comment() {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd()) advance();
}
//< comment
//> identifier
private void identifier() {
while (isAlphaNumeric(peek())) advance();
Expand Down Expand Up @@ -157,9 +152,8 @@ private void number() {
while (isDigit(peek())) advance();
}

double value = Double.parseDouble(
source.substring(start, current));
addToken(NUMBER, value);
addToken(NUMBER,
Double.parseDouble(source.substring(start, current)));
}
//< number
//> string
Expand Down Expand Up @@ -218,7 +212,7 @@ private boolean isAlphaNumeric(char c) {
//> is-digit
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
} // [is-digit]
//< is-digit
//> is-at-end
private boolean isAtEnd() {
Expand Down
2 changes: 1 addition & 1 deletion java/com/craftinginterpreters/lox/Token.java
Expand Up @@ -5,7 +5,7 @@ class Token {
final TokenType type;
final String lexeme;
final Object literal;
final int line;
final int line; // [location]

Token(TokenType type, String lexeme, Object literal, int line) {
this.type = type;
Expand Down
8 changes: 8 additions & 0 deletions note/chapter/parsing-expressions.md
Expand Up @@ -38,3 +38,11 @@ https://www.cs.cmu.edu/~pattis/misc/ebnf.pdf
- is an empty source file a valid program?
- write the bnf for a language you know. if the language is complex, just do
an interesting subset. what parts are hard?

--

error recovery:

The tricky part, of course, is that the first error may *cause* later **cascaded errors**. For example, if they accidentally started a string with `'` instead of `"`, then the rest of the string literal will likely cause a number of bogus syntax errors when the scanner and parser tries to treat it like code.

There is an art, called **error recovery** to getting back to a good state after an error is found to minimize the number of later spurious errors. We'll talk more about it during parsing.
6 changes: 6 additions & 0 deletions note/log.txt
@@ -1,3 +1,9 @@
2017/01/01 - 772 words design note for scanner, aside markers in code
2016/12/31 - 1081 words first draft scanner, mostly done
2016/12/30 - 1085 words first draft scanner
2016/12/29 - 722 words first draft scanner
2016/12/28 - 561 words first draft scanner
2016/12/27 - 1127 words first draft scanner
2016/12/26 - finish outlining and splitting, reallow multiline strings
2016/12/25 - fix some bugs in chapter splitting, make multiline strings and error
2016/12/24 - slice up more scanning code into snippets
Expand Down
2 changes: 1 addition & 1 deletion site/contents.html
Expand Up @@ -118,7 +118,6 @@ <h4><a href="the-lox-language.html#design-note">Design Note: Statements and Expr
<h2><span class="num">II.</span><a href="a-tree-walk-interpreter-in-java.html" name="a-tree-walk-interpreter-in-java">A Tree-Walk Interpreter in Java</a></h2>
<ol start="4">
<li><h3><a href="scanning.html">Scanning</a>
<a class="not-written" href="#not-written" title="This chapter hasn't been written yet">(coming soon!)</a>
</h3>
<p>
<span class="topic">Tokens</span>
Expand All @@ -129,6 +128,7 @@ <h2><span class="num">II.</span><a href="a-tree-walk-interpreter-in-java.html" n
<span class="topic">Reserved words</span>
<span class="topic">Error reporting</span>
</p>
<h4><a href="scanning.html#design-note">Design Note: Implicit Semicolons</a></h4>
</li>
<li><h3><a href="representing-code.html">Representing Code</a>
<a class="not-written" href="#not-written" title="This chapter hasn't been written yet">(coming soon!)</a>
Expand Down

0 comments on commit 127eed2

Please sign in to comment.