indention start
This commit is contained in:
parent
1c21aede4b
commit
5062a9b588
12 changed files with 268 additions and 48 deletions
2
makefile
2
makefile
|
|
@ -11,7 +11,7 @@ $(TARGET): build/parser.o build/lexer.o build/main.o
|
||||||
build/parser.cpp build/parser.hpp: src/parser.y | build
|
build/parser.cpp build/parser.hpp: src/parser.y | build
|
||||||
$(BISON) -d -o build/parser.cpp src/parser.y
|
$(BISON) -d -o build/parser.cpp src/parser.y
|
||||||
|
|
||||||
build/lexer.cpp build/lexer.hpp: src/lexer.l build/parser.hpp src/scanner.hpp | build
|
build/lexer.cpp build/lexer.hpp: src/lexer.l build/parser.hpp src/scanner.hpp src/indent_helper.hpp| build
|
||||||
$(FLEX) --header-file=build/lexer.hpp -o build/lexer.cpp src/lexer.l
|
$(FLEX) --header-file=build/lexer.hpp -o build/lexer.cpp src/lexer.l
|
||||||
|
|
||||||
build/%.o: build/%.cpp
|
build/%.o: build/%.cpp
|
||||||
|
|
|
||||||
65
src/indent_helper.hpp
Normal file
65
src/indent_helper.hpp
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
#pragma once
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
class IndentHelper
|
||||||
|
{
|
||||||
|
std::stack<int> indent_stack;
|
||||||
|
int pending_dedents = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
IndentHelper() { indent_stack.push(0); }
|
||||||
|
|
||||||
|
int processLine(int spaces)
|
||||||
|
{
|
||||||
|
int current = indent_stack.top();
|
||||||
|
|
||||||
|
if (spaces > current)
|
||||||
|
{
|
||||||
|
indent_stack.push(spaces);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (spaces == current)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pending_dedents = 0;
|
||||||
|
while (indent_stack.top() > spaces)
|
||||||
|
{
|
||||||
|
indent_stack.pop();
|
||||||
|
pending_dedents++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indent_stack.top() != spaces)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Indentation error");
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasPendingDedents() const
|
||||||
|
{
|
||||||
|
return pending_dedents > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void consumePendingDedent()
|
||||||
|
{
|
||||||
|
if (pending_dedents > 0)
|
||||||
|
{
|
||||||
|
pending_dedents--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int flushAllDedents()
|
||||||
|
{
|
||||||
|
int total_dedents = 0;
|
||||||
|
while (indent_stack.size() > 1)
|
||||||
|
{
|
||||||
|
indent_stack.pop();
|
||||||
|
total_dedents++;
|
||||||
|
}
|
||||||
|
return total_dedents;
|
||||||
|
}
|
||||||
|
};
|
||||||
52
src/lexer.l
52
src/lexer.l
|
|
@ -5,28 +5,64 @@
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include "scanner.hpp"
|
#include "scanner.hpp"
|
||||||
|
#include "indent_helper.hpp"
|
||||||
|
|
||||||
|
IndentHelper indent_helper;
|
||||||
|
bool at_line_start = true;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
^[ \t]* {
|
||||||
|
if (!at_line_start) {
|
||||||
|
REJECT;
|
||||||
|
}
|
||||||
|
at_line_start = false;
|
||||||
|
|
||||||
|
int spaces = 0;
|
||||||
|
for (int i = 0; i < yyleng; i++)
|
||||||
|
spaces += (yytext[i] == '\t') ? 8 : 1;
|
||||||
|
|
||||||
|
int result = indent_helper.processLine(spaces);
|
||||||
|
if (result > 0) return yy::parser::token::TOK_INDENT;
|
||||||
|
if (result < 0) return yy::parser::token::TOK_DEDENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
\n {
|
||||||
|
at_line_start = true;
|
||||||
|
return yy::parser::token::TOK_NEWLINE;
|
||||||
|
}
|
||||||
|
|
||||||
def { return yy::parser::token::TOK_KW_DEF; }
|
def { return yy::parser::token::TOK_KW_DEF; }
|
||||||
if { return yy::parser::token::TOK_KW_IF; }
|
if { return yy::parser::token::TOK_KW_IF; }
|
||||||
return { return yy::parser::token::TOK_KW_RETURN; }
|
return { return yy::parser::token::TOK_KW_RETURN; }
|
||||||
for { return yy::parser::token::TOK_KW_FOR; }
|
for { return yy::parser::token::TOK_KW_FOR; }
|
||||||
in { return yy::parser::token::TOK_KW_IF;}
|
in { return yy::parser::token::TOK_KW_IN; }
|
||||||
\n { return yy::parser::token::TOK_NEWLINE; }
|
|
||||||
\( { return yy::parser::token::TOK_L_PAREN; }
|
\( { return yy::parser::token::TOK_L_PAREN; }
|
||||||
\) { return yy::parser::token::TOK_R_PAREN; }
|
\) { return yy::parser::token::TOK_R_PAREN; }
|
||||||
\: { return yy::parser::token::TOK_COLON; }
|
\: { return yy::parser::token::TOK_COLON; }
|
||||||
\= { return yy::parser::token::TOK_ASSIGN; }
|
\= { return yy::parser::token::TOK_ASSIGN; }
|
||||||
\+ { return yy::parser::token::TOK_PLUS; }
|
\+ { return yy::parser::token::TOK_PLUS; }
|
||||||
\- { return yy::parser::token::TOK_MINUS; }
|
\- { return yy::parser::token::TOK_MINUS; }
|
||||||
\" { yymore(); BEGIN(STRING); }
|
|
||||||
[0-9]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_NUMBER; }
|
|
||||||
[_a-zA-Z]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
|
||||||
<<EOF>> { return yy::parser::token::TOK_EOF; }
|
|
||||||
. { /* the last rule, ignore anything else (there should be nothing) */}
|
|
||||||
|
|
||||||
|
\" { yymore(); BEGIN(STRING); }
|
||||||
<STRING>\"\" { yymore(); }
|
<STRING>\"\" { yymore(); }
|
||||||
<STRING>\" { BEGIN(INITIAL); yylval->as<std::string>() = yytext; return yy::parser::token::TOK_STRING; }
|
<STRING>\" { BEGIN(INITIAL); yylval->as<std::string>() = yytext; return yy::parser::token::TOK_STRING; }
|
||||||
<STRING>. { yymore(); }
|
<STRING>. { yymore(); }
|
||||||
%%
|
|
||||||
|
[0-9]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_NUMBER; }
|
||||||
|
[A-Za-z_][A-Za-z0-9_]* { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
||||||
|
|
||||||
|
|
||||||
|
<<EOF>> {
|
||||||
|
if (indent_helper.hasPendingDedents()) {
|
||||||
|
indent_helper.consumeDedent();
|
||||||
|
return yy::parser::token::TOK_DEDENT;
|
||||||
|
}
|
||||||
|
int d;
|
||||||
|
if ((d = indent_helper.flushDedent()) != 0) return yy::parser::token::TOK_DEDENT;
|
||||||
|
return yy::parser::token::TOK_EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
. { /* ignore other characters */ }
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@
|
||||||
%token <int> NUMBER
|
%token <int> NUMBER
|
||||||
%token ASSIGN PLUS MINUS L_PAREN R_PAREN COLON
|
%token ASSIGN PLUS MINUS L_PAREN R_PAREN COLON
|
||||||
%token KW_DEF KW_IF KW_RETURN KW_FOR KW_IN
|
%token KW_DEF KW_IF KW_RETURN KW_FOR KW_IN
|
||||||
%token EOF NEWLINE INDENT_UP INDENT_DOWN
|
%token EOF NEWLINE INDENT DEDENT
|
||||||
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
|
||||||
|
|
@ -4,28 +4,28 @@ L_PAREN
|
||||||
R_PAREN
|
R_PAREN
|
||||||
COLON
|
COLON
|
||||||
NEWLINE
|
NEWLINE
|
||||||
INDENT_UP
|
INDENT
|
||||||
KW_IF
|
KW_IF
|
||||||
IDENTIFIER condition
|
IDENTIFIER condition
|
||||||
COLON
|
COLON
|
||||||
NEWLINE
|
NEWLINE
|
||||||
INDENT_UP
|
INDENT
|
||||||
KW_RETURN
|
KW_RETURN
|
||||||
IDENTIFIER value
|
IDENTIFIER value
|
||||||
NEWLINE
|
NEWLINE
|
||||||
INDENT_DOWN
|
DEDENT
|
||||||
KW_FOR
|
KW_FOR
|
||||||
IDENTIFIER item
|
IDENTIFIER item
|
||||||
KW_IN
|
KW_IN
|
||||||
IDENTIFIER list
|
IDENTIFIER list
|
||||||
COLON
|
COLON
|
||||||
NEWLINE
|
NEWLINE
|
||||||
INDENT_UP
|
INDENT
|
||||||
IDENTIFIER print
|
IDENTIFIER print
|
||||||
L_PAREN
|
L_PAREN
|
||||||
IDENTIFIER item
|
IDENTIFIER item
|
||||||
R_PAREN
|
R_PAREN
|
||||||
NEWLINE
|
NEWLINE
|
||||||
INDENT_DOWN
|
DEDENT
|
||||||
INDENT_DOWN
|
DEDENT
|
||||||
EOF
|
EOF
|
||||||
|
|
@ -1,3 +1,12 @@
|
||||||
x == y != z < > <= >=
|
def outer():
|
||||||
a + b - c * d / e % f
|
def inner1():
|
||||||
( ) [ ] { } , : . ;
|
if cond1:
|
||||||
|
action1()
|
||||||
|
if cond2:
|
||||||
|
action2()
|
||||||
|
def inner2():
|
||||||
|
for i in range(3):
|
||||||
|
if cond3:
|
||||||
|
action3()
|
||||||
|
action4()
|
||||||
|
return result
|
||||||
|
|
|
||||||
73
test/lexer/3.out
Normal file
73
test/lexer/3.out
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
KW_DEF
|
||||||
|
IDENTIFIER outer
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
KW_DEF
|
||||||
|
IDENTIFIER inner1
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
KW_IF
|
||||||
|
IDENTIFIER cond1
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
IDENTIFIER action1
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
NEWLINE
|
||||||
|
DEDENT
|
||||||
|
KW_IF
|
||||||
|
IDENTIFIER cond2
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
IDENTIFIER action2
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
NEWLINE
|
||||||
|
DEDENT
|
||||||
|
DEDENT
|
||||||
|
KW_DEF
|
||||||
|
IDENTIFIER inner2
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
KW_FOR
|
||||||
|
IDENTIFIER i
|
||||||
|
KW_IN
|
||||||
|
IDENTIFIER range
|
||||||
|
L_PAREN
|
||||||
|
NUMBER 3
|
||||||
|
R_PAREN
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
KW_IF
|
||||||
|
IDENTIFIER cond3
|
||||||
|
COLON
|
||||||
|
NEWLINE
|
||||||
|
INDENT
|
||||||
|
IDENTIFIER action3
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
NEWLINE
|
||||||
|
DEDENT
|
||||||
|
IDENTIFIER action4
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
NEWLINE
|
||||||
|
DEDENT
|
||||||
|
DEDENT
|
||||||
|
KW_RETURN
|
||||||
|
IDENTIFIER result
|
||||||
|
NEWLINE
|
||||||
|
DEDENT
|
||||||
|
EOF
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
42 3.14 0xFF 0b1010
|
x == y != z < > <= >=
|
||||||
"hello" 'world' """multiline"""
|
a + b - c * d / e % f
|
||||||
'test' "escaped\"quote"
|
( ) [ ] { } , : . ;
|
||||||
|
|
|
||||||
34
test/lexer/4.out
Normal file
34
test/lexer/4.out
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
IDENTIFIER x
|
||||||
|
EQ
|
||||||
|
IDENTIFIER y
|
||||||
|
NE
|
||||||
|
IDENTIFIER z
|
||||||
|
LT
|
||||||
|
GT
|
||||||
|
LTE
|
||||||
|
GTE
|
||||||
|
NEWLINE
|
||||||
|
IDENTIFIER a
|
||||||
|
PLUS
|
||||||
|
IDENTIFIER b
|
||||||
|
MINUS
|
||||||
|
IDENTIFIER c
|
||||||
|
MULTIPLY
|
||||||
|
IDENTIFIER d
|
||||||
|
DIVIDE
|
||||||
|
IDENTIFIER e
|
||||||
|
MODULO
|
||||||
|
IDENTIFIER f
|
||||||
|
NEWLINE
|
||||||
|
L_PAREN
|
||||||
|
R_PAREN
|
||||||
|
L_BRACKET
|
||||||
|
R_BRACKET
|
||||||
|
L_BRACE
|
||||||
|
R_BRACE
|
||||||
|
COMMA
|
||||||
|
COLON
|
||||||
|
DOT
|
||||||
|
SEMICOLON
|
||||||
|
NEWLINE
|
||||||
|
EOF
|
||||||
|
|
@ -1,8 +1,3 @@
|
||||||
def calculate(a, b=10):
|
42 3.14 0xFF 0b1010
|
||||||
result = (a + b) * 2
|
"hello" 'world' """multiline"""
|
||||||
if result > 100:
|
'test' "escaped\"quote"
|
||||||
print("Large result:", result)
|
|
||||||
return result
|
|
||||||
|
|
||||||
# This is a comment
|
|
||||||
x = calculate(5, 15)
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
_private = "valid"
|
def calculate(a, b=10):
|
||||||
var123 = 456
|
result = (a + b) * 2
|
||||||
CamelCase = True
|
if result > 100:
|
||||||
snake_case = False
|
print("Large result:", result)
|
||||||
very_long_variable_name_123 = "test"
|
return result
|
||||||
|
|
||||||
|
# This is a comment
|
||||||
|
x = calculate(5, 15)
|
||||||
|
|
|
||||||
5
test/lexer/7.in
Normal file
5
test/lexer/7.in
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
_private = "valid"
|
||||||
|
var123 = 456
|
||||||
|
CamelCase = True
|
||||||
|
snake_case = False
|
||||||
|
very_long_variable_name_123 = "test"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue