ZigBrains/core/src/main/grammar/Zon.flex

158 lines
5 KiB
Text
Raw Normal View History

2024-10-28 15:22:57 +01:00
/*
2024-10-30 16:43:33 +01:00
* This file is part of ZigBrains.
2024-10-28 15:22:57 +01:00
*
2025-02-06 01:14:51 +01:00
* Copyright (C) 2023-2025 FalsePattern
2024-10-30 16:43:33 +01:00
* All Rights Reserved
2024-10-28 15:22:57 +01:00
*
2024-10-30 16:43:33 +01:00
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
2024-10-28 15:22:57 +01:00
*
2024-10-30 16:43:33 +01:00
* ZigBrains is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* ZigBrains is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ZigBrains. If not, see <https://www.gnu.org/licenses/>.
2024-10-28 15:22:57 +01:00
*/
package com.falsepattern.zigbrains.zon.lexer;
import com.intellij.lexer.FlexLexer;
import com.intellij.psi.tree.IElementType;
import static com.intellij.psi.TokenType.WHITE_SPACE;
import static com.intellij.psi.TokenType.BAD_CHARACTER;
import static com.falsepattern.zigbrains.zon.psi.ZonTypes.*;
%%
%class ZonFlexLexer
%implements FlexLexer
%function advance
%type IElementType
%unicode
CRLF=\R
WHITE_SPACE=[\s]+
2025-03-11 14:07:44 +01:00
bin=[01]
bin_="_"? {bin}
oct=[0-7]
oct_="_"? {oct}
2024-10-28 15:22:57 +01:00
hex=[0-9a-fA-F]
2025-03-11 14:07:44 +01:00
hex_="_"? {hex}
dec=[0-9]
dec_="_"? {dec}
bin_int={bin} {bin_}*
oct_int={oct} {oct_}*
dec_int={dec} {dec_}*
hex_int={hex} {hex_}*
char_char= \\ .
| [^\'\n]
string_char= \\ .
| [^\"\n]
all_nl_wrap=[^\n]* [ \n]*
all_no_nl=[^\n]+
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
FLOAT= "0x" {hex_int} "." {hex_int} ([pP] [-+]? {dec_int})?
| {dec_int} "." {dec_int} ([eE] [-+]? {dec_int})?
| "0x" {hex_int} [pP] [-+]? {dec_int}
| {dec_int} [eE] [-+]? {dec_int}
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
INTEGER= "0b" {bin_int}
| "0o" {oct_int}
| "0x" {hex_int}
| {dec_int}
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
IDENTIFIER_PLAIN=[A-Za-z_][A-Za-z0-9_]*
%state STR_LIT
%state STR_MULT_LINE
%state CHAR_LIT
%state ID_QUOT
%state UNT_SQUOT
%state UNT_DQUOT
%state LINE_CMT
2024-10-28 15:22:57 +01:00
%%
2025-03-11 14:07:44 +01:00
//Comments
<YYINITIAL> "//" { yybegin(LINE_CMT); }
<LINE_CMT> {all_nl_wrap} "//" { }
2025-03-23 12:12:57 +01:00
<LINE_CMT> {all_no_nl} { }
2025-03-11 14:07:44 +01:00
<LINE_CMT> \n { yybegin(YYINITIAL); return LINE_COMMENT; }
<LINE_CMT> <<EOF>> { yybegin(YYINITIAL); return LINE_COMMENT; }
//Symbols
2024-10-28 15:22:57 +01:00
<YYINITIAL> "." { return DOT; }
2025-03-11 14:07:44 +01:00
<YYINITIAL> "=" { return EQUAL; }
2024-10-28 15:22:57 +01:00
<YYINITIAL> "{" { return LBRACE; }
<YYINITIAL> "}" { return RBRACE; }
<YYINITIAL> "," { return COMMA; }
2025-03-11 14:07:44 +01:00
//Keywords
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
<YYINITIAL> "false" { return KEYWORD_FALSE; }
<YYINITIAL> "true" { return KEYWORD_TRUE; }
<YYINITIAL> "null" { return KEYWORD_NULL; }
<YYINITIAL> "nan" { return NUM_NAN; }
<YYINITIAL> "inf" { return NUM_INF; }
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
//Strings
2024-10-28 15:22:57 +01:00
2025-03-11 14:07:44 +01:00
<YYINITIAL> "'" { yybegin(CHAR_LIT); }
<CHAR_LIT> {char_char}*"'" { yybegin(YYINITIAL); return CHAR_LITERAL; }
<CHAR_LIT> <<EOF>> { yybegin(YYINITIAL); return BAD_SQUOT; }
<CHAR_LIT> [^] { yypushback(1); yybegin(UNT_SQUOT); }
<YYINITIAL> "\"" { yybegin(STR_LIT); }
<STR_LIT> {string_char}*"\"" { yybegin(YYINITIAL); return STRING_LITERAL_SINGLE; }
<STR_LIT> <<EOF>> { yybegin(YYINITIAL); return BAD_DQUOT; }
<STR_LIT> [^] { yypushback(1); yybegin(UNT_DQUOT); }
<YYINITIAL> "\\\\" { yybegin(STR_MULT_LINE); }
<STR_MULT_LINE> {all_nl_wrap} "\\\\" { }
<STR_MULT_LINE> {all_no_nl} { }
<STR_MULT_LINE> \n { yybegin(YYINITIAL); return STRING_LITERAL_MULTI; }
<STR_MULT_LINE> <<EOF>> { yybegin(YYINITIAL); return STRING_LITERAL_MULTI; }
//Numbers
<YYINITIAL> {FLOAT} { return FLOAT; }
<YYINITIAL> {INTEGER} { return INTEGER; }
//Identifiers
<YYINITIAL> {IDENTIFIER_PLAIN} { return IDENTIFIER; }
<YYINITIAL> "@\"" { yybegin(ID_QUOT); }
<ID_QUOT> {string_char}*"\"" { yybegin(YYINITIAL); return IDENTIFIER; }
<ID_QUOT> <<EOF>> { yybegin(YYINITIAL); return BAD_DQUOT; }
<ID_QUOT> [^] { yypushback(1); yybegin(UNT_DQUOT); }
//Error handling
<UNT_SQUOT> <<EOF>> { yybegin(YYINITIAL); return BAD_SQUOT; }
<UNT_SQUOT> {CRLF} { yybegin(YYINITIAL); return BAD_SQUOT; }
<UNT_SQUOT> {all_no_nl} { }
<UNT_DQUOT> <<EOF>> { yybegin(YYINITIAL); return BAD_DQUOT; }
<UNT_DQUOT> {CRLF} { yybegin(YYINITIAL); return BAD_DQUOT; }
<UNT_DQUOT> {all_no_nl} { }
//Misc
<YYINITIAL> {WHITE_SPACE} { return WHITE_SPACE; }
2024-10-28 15:22:57 +01:00
[^] { return BAD_CHARACTER; }