0% found this document useful (0 votes)
12 views4 pages

Parsing Numbers with Scientific Notation

This document discusses parsing numbers, comments, strings, and escape characters in a programming language. It includes code to: 1) Parse numbers with exponential notation and underscores in the number. 2) Parse multiline comments delimited by === and ===. 3) Parse strings allowing escape characters by replacing them during parsing.

Uploaded by

Gehad Omar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Parsing Numbers with Scientific Notation

This document discusses parsing numbers, comments, strings, and escape characters in a programming language. It includes code to: 1) Parse numbers with exponential notation and underscores in the number. 2) Parse multiline comments delimited by === and ===. 3) Parse strings allowing escape characters by replacing them during parsing.

Uploaded by

Gehad Omar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1- Adding e to the number ➔➔➔ 8.5e+3 , 6.4e-2 , 25.

43e4
private void number() {
while (isDigit(peek())) {
advance();
}
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek())) {
advance();
}
if (peek() == 'e' && (peekNext() == '+' || peekNext() == '-') && isDigit(peekNextNext())) {
advance();
advance();
} else if (peek() == 'e' && isDigit(peekNext())) {
advance();
}
while (isDigit(peek())) {
advance();
}
}
String value = [Link](start, current);
addToken(NUMBER, [Link](value));
}
char peekNextNext()
{
if (current + 2 >= [Link]()) {
return '\0';
}
return [Link](current + 2);
}
2- number have underscore (_) (only one underscore)

private void number() {


while (isDigit(peek()) || (peek()=='_' && isDigit(peekNext()))) advance();

// Look for a fractional part.


if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();

while (isDigit(peek()) || (peek()=='_' && isDigit(peekNext()))) advance();


}
String value = [Link](start,current);
value = [Link]("_", "");

addToken(NUMBER,
[Link](value));
}
3- ===asd test asd=== // multible line Comment
case '=':

if (peek() == '=' && peekNext() == '=') {

multibleLineComment();

} else {

addToken(match('=') ? EQUAL_EQUAL : EQUAL);

break;

void multibleLineComment() {

advance();

advance();

while ((peek() != '=' || peekNext() != '=' || peekNextNext() != '=') && !isAtEnd()) {

if (peek() == '\n')

line++;

advance();

if (isAtEnd())

[Link](line, "Unterminated Comment.");

return;

advance(); advance(); advance();

char peekNextNext()

if (current + 2 >= [Link]()) {

return '\0';

return [Link](current + 2);

}
4- String have escape Characters ➔ ➔ asd “test” asd
private void string() {

while ((peek() != '"'||previous()=='\\') && !isAtEnd()) {

if (peek() == '\n') {

line++;

advance();

if (isAtEnd()) {

[Link](line, "Unterminated string.");

return;

// The closing ".

advance();

// Trim the surrounding quotes.

String value = [Link](start + 1, current - 1);

value = [Link]('\\', '\0');

addToken(STRING, value);

char previous()

return [Link](current-1);

You might also like