[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 05/13] java: examples: prefer switch to chains of else-if
From: |
Akim Demaille |
Subject: |
[PATCH 05/13] java: examples: prefer switch to chains of else-if |
Date: |
Wed, 5 Feb 2020 18:04:57 +0100 |
* examples/java/calc/Calc.y, examples/java/simple/Calc.y: here.
* examples/java/simple/Calc.y: Use the tokenizer's handling of blanks.
---
examples/java/calc/Calc.y | 19 +++++++++----------
examples/java/simple/Calc.y | 19 ++++++++++---------
2 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/examples/java/calc/Calc.y b/examples/java/calc/Calc.y
index dc3f99df..7a682860 100644
--- a/examples/java/calc/Calc.y
+++ b/examples/java/calc/Calc.y
@@ -118,23 +118,22 @@ class CalcLexer implements Calc.Lexer {
start.set (end);
int ttype = st.nextToken ();
end.set (reader.getPosition ());
- if (ttype == st.TT_EOF)
- return EOF;
- else if (ttype == st.TT_EOL)
+ switch (ttype)
{
+ case StreamTokenizer.TT_EOF:
+ return EOF;
+ case StreamTokenizer.TT_EOL:
end.line += 1;
end.column = 0;
return (int) '\n';
- }
- else if (ttype == st.TT_WORD)
- {
+ case StreamTokenizer.TT_WORD:
yylval = new Integer (st.sval);
return NUM;
+ case ' ': case '\t':
+ return yylex ();
+ default:
+ return ttype;
}
- else if (st.ttype == ' ' || st.ttype == '\t')
- return yylex ();
- else
- return st.ttype;
}
}
diff --git a/examples/java/simple/Calc.y b/examples/java/simple/Calc.y
index 0337e49d..e6c39df9 100644
--- a/examples/java/simple/Calc.y
+++ b/examples/java/simple/Calc.y
@@ -76,6 +76,8 @@ class CalcLexer implements Calc.Lexer {
st = new StreamTokenizer (new InputStreamReader (is));
st.resetSyntax ();
st.eolIsSignificant (true);
+ st.whitespaceChars ('\t', '\t');
+ st.whitespaceChars (' ', ' ');
st.wordChars ('0', '9');
}
@@ -92,18 +94,17 @@ class CalcLexer implements Calc.Lexer {
public int yylex () throws IOException {
int ttype = st.nextToken ();
- if (ttype == st.TT_EOF)
- return EOF;
- else if (ttype == st.TT_EOL)
- return (int) '\n';
- else if (ttype == st.TT_WORD)
+ switch (ttype)
{
+ case StreamTokenizer.TT_EOF:
+ return EOF;
+ case StreamTokenizer.TT_EOL:
+ return (int) '\n';
+ case StreamTokenizer.TT_WORD:
yylval = new Integer (st.sval);
return NUM;
+ default:
+ return ttype;
}
- else if (st.ttype == ' ' || st.ttype == '\t')
- return yylex ();
- else
- return st.ttype;
}
}
--
2.25.0
- [PATCH 00/13] Clean up the Java skeleton and tests, Akim Demaille, 2020/02/05
- [PATCH 03/13] traces: don't print the stack before the gotos, Akim Demaille, 2020/02/05
- [PATCH 01/13] traces: write the "Reading a token" alone on its line, Akim Demaille, 2020/02/05
- [PATCH 02/13] traces: show the stack after reading a token, Akim Demaille, 2020/02/05
- [PATCH 05/13] java: examples: prefer switch to chains of else-if,
Akim Demaille <=
- [PATCH 04/13] java: examples: split in two, Akim Demaille, 2020/02/05
- [PATCH 06/13] java: examples: fix the tracking of locations, Akim Demaille, 2020/02/05
- [PATCH 07/13] java: style: prefer putting the square brackets on the type, Akim Demaille, 2020/02/05
- [PATCH 08/13] java: style: avoid useless initializers, Akim Demaille, 2020/02/05
- [PATCH 09/13] java: tests: prepare the replacement of calculator tests, Akim Demaille, 2020/02/05
- [PATCH 10/13] java: tests: check location tracking in the calculator, Akim Demaille, 2020/02/05
- [PATCH 12/13] java: tests: check push parsers, Akim Demaille, 2020/02/05
- [PATCH 11/13] java: tests: remove now redundant tests, Akim Demaille, 2020/02/05
- [PATCH 13/13] java: tests: remove now redundant tests, Akim Demaille, 2020/02/05