Files
carbon-lang/executable_semantics/syntax/lexer.lpp
T
Dave Abrahams 5884aa1a5c Give syntax files more useful and mnemonic names.
Distinguishes parts that come from the parser and lexer. It used to be that all
the files were called "syntax*", but lexing and parsing are distinct phases that
are easier to keep track of when distinguished.  syntax.yy.cpp being the source
file generated by flex, containing the lexer was particularly confusing, because
the yy tends to indicate it is a yacc/Bison product, and the ".tab." substring,
indicating "tables" is not really useful to the developer.

These names also match up with what Bison's C++ example uses, which will make
the transition easier.
2021-03-04 18:45:54 -08:00

96 lines
2.0 KiB
Plaintext

/*
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
%{
#include <cstdlib>
#include "executable_semantics/syntax/parser.h"
%}
/* Turn off legacy bits we don't need */
%option noyywrap nounput nodefault noinput
/* maintains the number of the current line read from input in the
global variable yylineno.
*/
%option yylineno
AND "and"
ARROW "->"
AUTO "auto"
BOOL "Bool"
BREAK "break"
CASE "case"
CHOICE "choice"
COMMENT \/\/[^\n]*\n
CONTINUE "continue"
DBLARROW "=>"
DEFAULT "default"
ELSE "else"
EQUAL "=="
FALSE "false"
FN "fn"
FNTY "fnty"
IF "if"
INT "Int"
MATCH "match"
NOT "not"
OR "or"
RETURN "return"
STRUCT "struct"
TRUE "true"
TYPE "Type"
VAR "var"
WHILE "while"
identifier [A-Za-z_][A-Za-z0-9_]*
integer_literal [0-9]+
%%
{AND} { return AND; }
{ARROW} { return ARROW; }
{AUTO} { return AUTO; }
{BOOL} { return BOOL; }
{BREAK} { return BREAK; }
{CASE} { return CASE; }
{CHOICE} { return CHOICE; }
{COMMENT} ;
{CONTINUE} { return CONTINUE; }
{DBLARROW} { return DBLARROW; }
{DEFAULT} { return DEFAULT; }
{ELSE} { return ELSE; }
{EQUAL} { return EQUAL; }
{FALSE} { return FALSE; }
{FN} { return FN; }
{FNTY} { return FNTY; }
{IF} { return IF; }
{INT} { return INT; }
{MATCH} { return MATCH; }
{NOT} { return NOT; }
{OR} { return OR; }
{RETURN} { return RETURN; }
{STRUCT} { return STRUCT; }
{TRUE} { return TRUE; }
{TYPE} { return TYPE; }
{VAR} { return VAR; }
{WHILE} { return WHILE; }
{identifier} {
int n = strlen(yytext);
yylval.str = reinterpret_cast<char*>(malloc((n + 1) * sizeof(char)));
strncpy(yylval.str, yytext, n + 1);
return identifier;
}
{integer_literal} {
yylval.num = atof(yytext);
return integer_literal;
}
[ \t\n]+ ;
. { return yytext[0]; }
%%