Skip to content

Commit

Permalink
Merge pull request #8 from wangziwenhk/dev
Browse files Browse the repository at this point in the history
使用自定义的解析器
  • Loading branch information
wangziwenhk authored Oct 8, 2024
2 parents c875970 + c805be6 commit 158bc4f
Show file tree
Hide file tree
Showing 32 changed files with 1,520 additions and 1,140 deletions.
2 changes: 2 additions & 0 deletions RiddleLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ False:'false';
Static:'static';
Const:'const';
Null:'null';
Try:'try';
Catch:'catch';
//可见字符
//基本运算符
LeftBracket: '(';
Expand Down
28 changes: 19 additions & 9 deletions RiddleParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ options {
}

@Header{
}

@parserFile::members {
}

null_cnt
: Semi
| Endl
;

program
: statement_ed*
| EOF
Expand All @@ -31,10 +37,15 @@ statement
| whileStatement
| ifStatement
| returnStatement
| tryExpr
| expression
| LeftCurly statement_ed* RightCurly
;

bodyExpr
: statement_ed*
;

packStatement
: Package packName=id
;
Expand All @@ -58,13 +69,8 @@ defineArgs
;

funcDefine
: Func funcName=Identifier LeftBracket args=defineArgs RightBracket (Sub Greater returnType=typeName)? LeftCurly body=funcBody RightCurly
: Func funcName=Identifier LeftBracket args=defineArgs RightBracket (Sub Greater returnType=typeName)? LeftCurly body=bodyExpr RightCurly
;

funcBody
: statement_ed*
;

forStatement
: For LeftBracket (init=varDefineStatement)? Semi (termCond=expression)? Semi (selfVar=statement)? RightBracket body=statement_ed
;
Expand All @@ -83,11 +89,15 @@ returnStatement
;

classDefine
: Class className = id LeftCurly body=classBody RightCurly
: Class className = id LeftCurly body=bodyExpr RightCurly
;

classBody
: statement_ed*
tryExpr
: Try LeftCurly tryBody=bodyExpr RightCurly null_cnt? catchExpr
;

catchExpr
: Catch LeftBracket varDefineStatement RightBracket
;

// 这一块就是使用
Expand Down
307 changes: 156 additions & 151 deletions parser/RiddleLexer.cpp

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions parser/RiddleLexer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Generated from E:/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.1
// Generated from E:/Riddle-Language/RiddleLexer.g4 by ANTLR 4.13.2

#pragma once

Expand All @@ -15,15 +15,15 @@ class RiddleLexer : public antlr4::Lexer {
Var = 1, Val = 2, For = 3, While = 4, If = 5, Else = 6, Func = 7, Return = 8,
Import = 9, Package = 10, Class = 11, Public = 12, Protected = 13, Private = 14,
Override = 15, True = 16, False = 17, Static = 18, Const = 19, Null = 20,
LeftBracket = 21, RightBracket = 22, LeftSquare = 23, RightSquare = 24,
LeftCurly = 25, RightCurly = 26, Colon = 27, Semi = 28, Comma = 29,
Equal = 30, Assign = 31, Greater = 32, Less = 33, LeftLeft = 34, RightRight = 35,
RightRightRight = 36, Add = 37, Sub = 38, Star = 39, Div = 40, Mod = 41,
Not = 42, And = 43, Or = 44, Xor = 45, Dot = 46, DoubleQuotes = 47,
Quotes = 48, Endl = 49, Identifier = 50, Hexadecimal = 51, Decimal = 52,
Octal = 53, Binary = 54, Float = 55, IntegerSequence = 56, HEX_DIGIT = 57,
OCTAL_DIGIT = 58, BINARY_DIGIT = 59, DIGIT = 60, STRING = 61, LINE_COMMENT = 62,
BLOCK_COMMENT = 63, WHITESPACE = 64
Try = 21, Catch = 22, LeftBracket = 23, RightBracket = 24, LeftSquare = 25,
RightSquare = 26, LeftCurly = 27, RightCurly = 28, Colon = 29, Semi = 30,
Comma = 31, Equal = 32, Assign = 33, Greater = 34, Less = 35, LeftLeft = 36,
RightRight = 37, RightRightRight = 38, Add = 39, Sub = 40, Star = 41,
Div = 42, Mod = 43, Not = 44, And = 45, Or = 46, Xor = 47, Dot = 48,
DoubleQuotes = 49, Quotes = 50, Endl = 51, Identifier = 52, Hexadecimal = 53,
Decimal = 54, Octal = 55, Binary = 56, Float = 57, IntegerSequence = 58,
HEX_DIGIT = 59, OCTAL_DIGIT = 60, BINARY_DIGIT = 61, DIGIT = 62, STRING = 63,
LINE_COMMENT = 64, BLOCK_COMMENT = 65, WHITESPACE = 66
};

explicit RiddleLexer(antlr4::CharStream *input);
Expand Down
8 changes: 7 additions & 1 deletion parser/RiddleLexer.interp

Large diffs are not rendered by default.

150 changes: 77 additions & 73 deletions parser/RiddleLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,52 @@ False=17
Static=18
Const=19
Null=20
LeftBracket=21
RightBracket=22
LeftSquare=23
RightSquare=24
LeftCurly=25
RightCurly=26
Colon=27
Semi=28
Comma=29
Equal=30
Assign=31
Greater=32
Less=33
LeftLeft=34
RightRight=35
RightRightRight=36
Add=37
Sub=38
Star=39
Div=40
Mod=41
Not=42
And=43
Or=44
Xor=45
Dot=46
DoubleQuotes=47
Quotes=48
Endl=49
Identifier=50
Hexadecimal=51
Decimal=52
Octal=53
Binary=54
Float=55
IntegerSequence=56
HEX_DIGIT=57
OCTAL_DIGIT=58
BINARY_DIGIT=59
DIGIT=60
STRING=61
LINE_COMMENT=62
BLOCK_COMMENT=63
WHITESPACE=64
Try=21
Catch=22
LeftBracket=23
RightBracket=24
LeftSquare=25
RightSquare=26
LeftCurly=27
RightCurly=28
Colon=29
Semi=30
Comma=31
Equal=32
Assign=33
Greater=34
Less=35
LeftLeft=36
RightRight=37
RightRightRight=38
Add=39
Sub=40
Star=41
Div=42
Mod=43
Not=44
And=45
Or=46
Xor=47
Dot=48
DoubleQuotes=49
Quotes=50
Endl=51
Identifier=52
Hexadecimal=53
Decimal=54
Octal=55
Binary=56
Float=57
IntegerSequence=58
HEX_DIGIT=59
OCTAL_DIGIT=60
BINARY_DIGIT=61
DIGIT=62
STRING=63
LINE_COMMENT=64
BLOCK_COMMENT=65
WHITESPACE=66
'var'=1
'val'=2
'for'=3
Expand All @@ -82,32 +84,34 @@ WHITESPACE=64
'static'=18
'const'=19
'null'=20
'('=21
')'=22
'['=23
']'=24
'{'=25
'}'=26
':'=27
';'=28
','=29
'=='=30
'='=31
'>'=32
'<'=33
'<<'=34
'>>'=35
'>>>'=36
'+'=37
'-'=38
'*'=39
'/'=40
'%'=41
'!'=42
'&'=43
'|'=44
'^'=45
'.'=46
'"'=47
'\''=48
'\n'=49
'try'=21
'catch'=22
'('=23
')'=24
'['=25
']'=26
'{'=27
'}'=28
':'=29
';'=30
','=31
'=='=32
'='=33
'>'=34
'<'=35
'<<'=36
'>>'=37
'>>>'=38
'+'=39
'-'=40
'*'=41
'/'=42
'%'=43
'!'=44
'&'=45
'|'=46
'^'=47
'.'=48
'"'=49
'\''=50
'\n'=51
Loading

0 comments on commit 158bc4f

Please sign in to comment.