Skip to content

Commit

Permalink
Add Flex package. (ruslo#1039)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaachier authored and ruslo committed Sep 16, 2017
1 parent cbec529 commit 7a39322
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake/configs/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ hunter_config(eos VERSION 0.12.1)
hunter_config(FakeIt VERSION 2.0.3)
hunter_config(fixesproto VERSION 5.0)
hunter_config(flatbuffers VERSION 1.3.0-p3)
hunter_config(flex VERSION 2.6.4)
hunter_config(fmt VERSION 4.0.0)
hunter_config(freetype VERSION 2.6.2)
hunter_config(gauze VERSION 0.1.1)
Expand Down
23 changes: 23 additions & 0 deletions cmake/projects/flex/hunter.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# !!! DO NOT PLACE HEADER GUARDS HERE !!!

include(hunter_add_version)
include(hunter_cacheable)
include(hunter_configuration_types)
include(hunter_download)
include(hunter_pick_scheme)

hunter_add_version(
PACKAGE_NAME
flex
VERSION
"2.6.4"
URL
"https://github.com/westes/flex/files/981163/flex-2.6.4.tar.gz"
SHA1
fafece095a0d9890ebd618adb1f242d8908076e1
)

hunter_configuration_types(flex CONFIGURATION_TYPES Release)
hunter_pick_scheme(DEFAULT url_sha1_autotools)
hunter_cacheable(flex)
hunter_download(PACKAGE_NAME flex)
1 change: 1 addition & 0 deletions docs/packages/compiler.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Compiler
--------

- :ref:`pkg.flex` - a tool for generating scanners.
- :ref:`pkg.LLVM` - collection of modular and reusable compiler and toolchain technologies.
44 changes: 44 additions & 0 deletions docs/packages/pkg/flex.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. spelling::

flex

.. index:: compiler ; flex

.. _pkg.flex:

flex
=====

- `Official <https://github.com/westes/flex>`__
- `Example <https://github.com/ruslo/hunter/blob/master/examples/flex/CMakeLists.txt>`__
- Added by `Isaac Hier <https://github.com/isaachier>`__ (`pr-1039 <https://github.com/ruslo/hunter/pull/1039>`__)

Simple flex example (no bison).

.. code-block:: cmake
hunter_add_package(flex)
find_package(FLEX REQUIRED)
FLEX_TARGET(MyScanner numbers.lex ${CMAKE_CURRENT_BINARY_DIR}/numbers.cpp)
add_executable(main ${FLEX_MyScanner_OUTPUTS})
target_include_directories(main PUBLIC ${FLEX_INCLUDE_DIRS})
target_link_libraries(main ${FLEX_LIBRARIES})
More complex example involving flex and bison. Based on `FindFLEX <https://cmake.org/cmake/help/v3.0/module/FindFLEX.html>`__.

.. code-block:: cmake
find_package(BISON REQUIRED)
hunter_add_package(flex)
find_package(FLEX REQUIRED)
BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp)
FLEX_TARGET(MyScanner lexer.l ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp)
ADD_FLEX_BISON_DEPENDENCY(MyScanner MyParser) # MyParser defines tokens for MyScanner
add_executable(main main.cpp ${BISON_MyParser_OUTPUTS} ${FLEX_MyScanner_OUTPUTS})
target_include_directories(main
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
${BISON_INCLUDE_DIRS}
${FLEX_INCLUDE_DIRS})
target_link_libraries(main ${BISON_LIBRARIES} ${FLEX_LIBRARIES})
13 changes: 13 additions & 0 deletions examples/flex/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.0)

include("../common.cmake")

project(download-flex)

hunter_add_package(flex)
find_package(FLEX REQUIRED)

FLEX_TARGET(MyScanner numbers.lex ${CMAKE_CURRENT_BINARY_DIR}/numbers.cpp)
add_executable(main ${FLEX_MyScanner_OUTPUTS})
target_include_directories(main PUBLIC ${FLEX_INCLUDE_DIRS})
target_link_libraries(main ${FLEX_LIBRARIES})
145 changes: 145 additions & 0 deletions examples/flex/numbers.lex
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* numbers.lex : An example of the definitions and techniques
* for scanning numbers
*/

%{
#include <stdio.h>

#define UNSIGNED_LONG_SYM 1
#define SIGNED_LONG_SYM 2
#define UNSIGNED_SYM 3
#define SIGNED_SYM 4
#define LONG_DOUBLE_SYM 5
#define FLOAT_SYM 6

union _yylval {
long double ylong_double;
float yfloat;
unsigned long yunsigned_long;
unsigned yunsigned;
long ysigned_long;
int ysigned;
} yylval;

%}

digit [0-9]
hex_digit [0-9a-fA-F]
oct_digit [0-7]

exponent [eE][+-]?{digit}+
i {digit}+
float_constant ({i}\.{i}?|{i}?\.{i}){exponent}?
hex_constant 0[xX]{hex_digit}+
oct_constant 0{oct_digit}*
int_constant {digit}+
long_ext [lL]
unsigned_ext [uU]
float_ext [fF]
ulong_ext {long_ext}{unsigned_ext}|{unsigned_ext}{long_ext}

%%

{hex_constant}{ulong_ext} { /* we need to skip the "0x" part */
sscanf(&yytext[2],"%lx",&yylval.yunsigned_long);
return(UNSIGNED_LONG_SYM);
}
{hex_constant}{long_ext} {
sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{hex_constant}{unsigned_ext} {
sscanf(&yytext[2],"%x",&yylval.yunsigned);
return(UNSIGNED_SYM);
}
{hex_constant} { /* use %lx to protect against overflow */
sscanf(&yytext[2],"%lx",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{oct_constant}{ulong_ext} {
sscanf(yytext,"%lo",&yylval.yunsigned_long);
return(UNSIGNED_LONG_SYM);
}
{oct_constant}{long_ext} {
sscanf(yytext,"%lo",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{oct_constant}{unsigned_ext} {
sscanf(yytext,"%o",&yylval.yunsigned);
return(UNSIGNED_SYM);
}
{oct_constant} { /* use %lo to protect against overflow */
sscanf(yytext,"%lo",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{int_constant}{ulong_ext} {
sscanf(yytext,"%ld",&yylval.yunsigned_long);
return(UNSIGNED_LONG_SYM);
}
{int_constant}{long_ext} {
sscanf(yytext,"%ld",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{int_constant}{unsigned_ext} {
sscanf(yytext,"%d",&yylval.yunsigned);
return(UNSIGNED_SYM);
}
{int_constant} { /* use %ld to protect against overflow */
sscanf(yytext,"%ld",&yylval.ysigned_long);
return(SIGNED_LONG_SYM);
}
{float_constant}{long_ext} {
sscanf(yytext,"%Lf",&yylval.ylong_double);
return(LONG_DOUBLE_SYM);
}
{float_constant}{float_ext} {
sscanf(yytext,"%f",&yylval.yfloat);
return(FLOAT_SYM);
}
{float_constant} { /* use %Lf to protect against overflow */
sscanf(yytext,"%Lf",&yylval.ylong_double);
return(LONG_DOUBLE_SYM);
}
%%

int main(void)
{
int code;

while((code = yylex())){
printf("yytext : %s\n",yytext);
switch(code){
case UNSIGNED_LONG_SYM:
printf("Type of number : UNSIGNED LONG\n");
printf("Value of number : %lu\n",yylval.yunsigned_long);
break;
case SIGNED_LONG_SYM:
printf("Type of number : SIGNED LONG\n");
printf("Value of number : %ld\n",yylval.ysigned_long);
break;
case UNSIGNED_SYM:
printf("Type of number : UNSIGNED\n");
printf("Value of number : %u\n",yylval.yunsigned);
break;
case SIGNED_SYM:
printf("Type of number : SIGNED\n");
printf("Value of number : %d\n",yylval.ysigned);
break;
case LONG_DOUBLE_SYM:
printf("Type of number : LONG DOUBLE\n");
printf("Value of number : %Lf\n",yylval.ylong_double);
break;
case FLOAT_SYM:
printf("Type of number : FLOAT\n");
printf("Value of number : %f\n",yylval.yfloat);
break;
default:
printf("Type of number : UNDEFINED\n");
printf("Value of number : UNDEFINED\n");
break;
}
}
return(0);
}

0 comments on commit 7a39322

Please sign in to comment.