├── tests ├── preprocessor │ ├── test_files │ │ ├── inc_test_1.h │ │ ├── inc_test_2.h │ │ ├── inc_test_3.h │ │ ├── first_script.h │ │ └── evals_and_macros.h │ ├── test_eval_macros.cpp │ ├── test_includes.cpp │ └── test_misc.cpp ├── lexer │ ├── lex_test_4.txt │ ├── lex_test_2.txt │ ├── lex_test_1.txt │ ├── lex_test_5.ini │ ├── lex_test_6.txt │ ├── lex_test_3.txt │ ├── simple_ini_parser.cpp │ ├── simple_cmdline_parser.cpp │ └── misc_lex_tests.cpp └── vs2015 │ ├── tests_vs2015.vcxproj.filters │ ├── tests_vs2015.sln │ ├── main.cpp │ └── tests_vs2015.vcxproj ├── README.md └── LICENSE /tests/preprocessor/test_files/inc_test_1.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | const int date = 2016-5-23; 5 | const char * s = "These are the contents of \'inc_test_1.h\'"; 6 | 7 | -------------------------------------------------------------------------------- /tests/preprocessor/test_files/inc_test_2.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | const int date = 2016-5-23; 5 | const char * s = "These are the contents of \'inc_test_2.h\'"; 6 | 7 | #include 8 | 9 | "\tSome more stuff from inc_test_2...\n"; 10 | 11 | -------------------------------------------------------------------------------- /tests/preprocessor/test_files/inc_test_3.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #ifndef INC_TEST_3_H_ 5 | #define INC_TEST_3_H_ 6 | 7 | const int date = 2016-5-23; 8 | const char * s = "These are the contents of \'inc_test_3.h\'"; 9 | 10 | #define TEST_DEFINE_INC3 "Hello from include file #3\n" 11 | printf("%s", TEST_DEFINE_INC3); 12 | 13 | #endif // INC_TEST_3_H_ 14 | -------------------------------------------------------------------------------- /tests/preprocessor/test_files/first_script.h: -------------------------------------------------------------------------------- 1 | 2 | // File including several others... 3 | #pragma once 4 | 5 | 6 | #include 7 | 8 | 9 | 10 | #include 11 | 12 | 13 | 14 | // This one is already included from inc_test_2, so it won't be expanded again in here. 15 | #include 16 | 17 | 18 | 19 | "The end"; 20 | 21 | 22 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_4.txt: -------------------------------------------------------------------------------- 1 | 2 | // A trailing comma after the last value is ignored, as it is in C. 3 | // Delimiters can be any user defined punctuation. Below we use [], {} and (). 4 | 5 | // 1D matrix ('[]' delimiters): 6 | [ 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0, ] 7 | 8 | // 2D matrix ('{}' delimiters): 9 | { 10 | { 1.0, 2.0, 3.0 }, 11 | { 4.0, 5.0, 6.0 }, 12 | { 7.0, 8.0, 9.0 }, 13 | } 14 | 15 | // 3D matrix ('()' delimiters): 16 | ( 17 | ( 18 | ( 1, 2, 3 ), 19 | ( 4, 5, 6 ), 20 | ), 21 | ( 22 | ( 7, 8, 9 ), 23 | ( 10, 11, 12 ), 24 | ), 25 | ) 26 | 27 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_2.txt: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Test with one of each of the default supported punctuation characters/strings: 4 | */ 5 | 6 | = 7 | + 8 | - 9 | * 10 | / 11 | % 12 | >> 13 | << 14 | += 15 | -= 16 | *= 17 | /= 18 | %= 19 | >>= 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 | \ 49 | ( 50 | ) 51 | [ 52 | ] 53 | { 54 | } 55 | # 56 | ## 57 | $ 58 | 59 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_1.txt: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Types on the left will be scanned as the type in right-hand-side comment. 4 | */ 5 | 6 | 'x' // char literal (string) 7 | '4cc' // multi-char literal (string) 8 | "hello world" // string 9 | 0 // bool 10 | 1 // bool 11 | false // bool 12 | true // bool 13 | -120 // char 14 | -31000 // short 15 | -4096 // signed int 16 | 0x11AABBCC // signed long 17 | 6789UL // unsigned int 18 | 0x9908B0DFul // unsigned long 19 | 0x12 // uint8_t 20 | 0xEEFF // uint16_t 21 | 0xDEADBEEF // uint32_t 22 | 0xCAFED00DCAFEBABE // uint64_t 23 | 2.718281828f // float 24 | 3.14159265358979323846L // double 25 | 26 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_5.ini: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Sample INI config file for the simple_ini_parser test. 4 | # 5 | 6 | # A global parameter: 7 | version = 1.3 8 | 9 | # Some made-up rendering presets: 10 | [render_presets] 11 | renderer_backend = "vulkan" 12 | enable_fxaa = true 13 | enable_ssao = true 14 | enable_motion_blur = false 15 | enable_gamma_correction = true 16 | shadows_quality_percent = 85.0 17 | texture_memory_gb = 1.5 18 | screen_width = 1440 19 | screen_height = 900 20 | 21 | # Some general app settings: 22 | [app_settings] 23 | fullscreen = true 24 | allow_minimize = false 25 | window_title = "My super fun 3D game!" 26 | scaling_ratio = 2.0 27 | 28 | # Network settings -> IP and port: 29 | [net_settings] 30 | server_ip = 172.16.254.1:8080 31 | server_port_alt = 4000 32 | 33 | ; A semicolon is also a valid INI comment 34 | 35 | -------------------------------------------------------------------------------- /tests/vs2015/tests_vs2015.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | 14 | 15 | Header Files 16 | 17 | 18 | Header Files 19 | 20 | 21 | 22 | 23 | Source Files 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_6.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | * Test for a custom "verbose" punctuation table. 5 | * Each of the <> enclosed words maps to a C/C++ punctuation. 6 | */ 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 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 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Parse Utils 3 | 4 | C++11 utilities for lexing and parsing of script languages, configuration files and command-lines. 5 | 6 | `lexer.hpp` contains a lightweight lexicographical scanner capable of scanning C-like languages. 7 | Code was adapted from the lexer used by the id Tech 4 engine and the DOOM 3 game [(available here)](https://github.com/id-Software/DOOM-3-BFG/blob/master/neo/idlib/Lexer.h). 8 | 9 | `preprocessor.hpp` contains a (mostly) feature-complete preprocessor for C-like languages. 10 | It supports include files, conditional directives, macro constants, function-like macros, variadic macros and the `$eval()` 11 | extension capable of resolving simple arithmetical and logical expressions. This class is loosely based on `idParser` from id Tech 4. 12 | 13 | The C++ classes are header only and self contained. You have to include the `.hpp` in one source file 14 | and define `XYZ_IMPLEMENTATION` to generate the implementation code in that source file. After that, 15 | the header can be used as a normal C++ header. This is in the same spirit of the [stb](https://github.com/nothings/stb) libraries. 16 | 17 | See the `tests/` directory for usage examples and test code. 18 | 19 | ### License 20 | 21 | Code in this repository is released under the terms of the GNU GPL version 3, 22 | to comply with the license used by code this work was based on. 23 | 24 | -------------------------------------------------------------------------------- /tests/vs2015/tests_vs2015.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tests_vs2015", "tests_vs2015.vcxproj", "{A7270887-0A29-4B3E-BAE0-F039873ACE09}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Debug|x64.ActiveCfg = Debug|x64 17 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Debug|x64.Build.0 = Debug|x64 18 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Debug|x86.ActiveCfg = Debug|Win32 19 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Debug|x86.Build.0 = Debug|Win32 20 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Release|x64.ActiveCfg = Release|x64 21 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Release|x64.Build.0 = Release|x64 22 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Release|x86.ActiveCfg = Release|Win32 23 | {A7270887-0A29-4B3E-BAE0-F039873ACE09}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /tests/preprocessor/test_eval_macros.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: test_eval_macros.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 25/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Test preprocessing a file with lots of $eval()s and some #defined macros. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -Wshadow -pedantic -I../../ test_eval_macros.cpp -o pp_test_eval_macros 13 | 14 | #define LEXER_IMPLEMENTATION 15 | #include "lexer.hpp" 16 | 17 | #define PREPROCESSOR_IMPLEMENTATION 18 | #include "preprocessor.hpp" 19 | 20 | #include 21 | #include 22 | 23 | int main() 24 | { 25 | preprocessor pp; 26 | 27 | if (!pp.init_from_file("test_files/evals_and_macros.h", preprocessor::flags::warn_macro_redefinitions)) 28 | { 29 | std::cerr << "Can't open " << pp.get_current_script()->get_filename() << "\n"; 30 | return EXIT_FAILURE; 31 | } 32 | 33 | std::string result; 34 | if (!pp.preprocess(&result)) 35 | { 36 | std::cerr << "Failed to preprocess file " << pp.get_current_script()->get_filename() << "\n"; 37 | return EXIT_FAILURE; 38 | } 39 | 40 | std::cout << "\n"; 41 | std::cout << result << "\n"; 42 | std::cout << "\n"; 43 | } 44 | -------------------------------------------------------------------------------- /tests/preprocessor/test_includes.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: test_includes.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 25/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Testing #include resolution with the preprocessor class. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -Wshadow -pedantic -I../../ test_includes.cpp -o pp_test_inc 13 | 14 | #define LEXER_IMPLEMENTATION 15 | #include "lexer.hpp" 16 | 17 | #define PREPROCESSOR_IMPLEMENTATION 18 | #include "preprocessor.hpp" 19 | 20 | #include 21 | #include 22 | 23 | int main() 24 | { 25 | preprocessor pp; 26 | pp.add_default_search_path("test_files/"); 27 | 28 | if (!pp.init_from_file("test_files/first_script.h", preprocessor::flags::warn_macro_redefinitions)) 29 | { 30 | std::cerr << "Can't open " << pp.get_current_script()->get_filename() << "\n"; 31 | return EXIT_FAILURE; 32 | } 33 | 34 | std::string result; 35 | if (!pp.preprocess(&result)) 36 | { 37 | std::cerr << "Failed to preprocess file " << pp.get_current_script()->get_filename() << "\n"; 38 | return EXIT_FAILURE; 39 | } 40 | 41 | std::cout << "\n"; 42 | std::cout << result << "\n"; 43 | std::cout << "\n"; 44 | } 45 | -------------------------------------------------------------------------------- /tests/lexer/lex_test_3.txt: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Test data (http://www.lipsum.com/) for a line and word counter using the lexer class: 42 lines, 527 words. 4 | */ 5 | 6 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id eros sagittis, malesuada dolor eget, interdum sem. 7 | Fusce pharetra ultricies mi sed tristique. Pellentesque a ipsum sit amet orci ultricies ultrices. Sed et ultrices sem, 8 | eget cursus arcu. Vivamus vel placerat elit. Morbi tincidunt justo lacus, a commodo elit ultricies eu. Pellentesque 9 | habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec aliquet ex nibh. Vivamus bibendum 10 | porta risus, nec rutrum turpis vehicula ut. Vestibulum vel erat semper, tempor nibh sit amet, bibendum quam. Mauris dolor 11 | ipsum, tincidunt id egestas in, convallis vitae augue. Aliquam pellentesque pharetra fermentum. 12 | 13 | Suspendisse urna dolor, finibus a dolor sit amet, tincidunt tincidunt dolor. Ut faucibus odio ut sem aliquet, vitae 14 | tincidunt nisl placerat. Phasellus mattis consequat dolor at volutpat. In nisi lacus, blandit vel cursus a, tincidunt 15 | cursus sem. Morbi consectetur, dui eu faucibus pretium, felis augue egestas orci, a porta dolor sem in metus. Nullam 16 | eu lacus eleifend, lacinia urna nec, pharetra massa. Morbi a placerat urna, vitae lobortis tortor. Duis commodo 17 | condimentum neque, eu mattis metus fringilla vel. Praesent accumsan in mi nec mollis. Pellentesque consequat ligula 18 | in rhoncus rhoncus. Quisque non sem purus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur 19 | ridiculus mus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus 20 | condimentum, odio eget tempor rhoncus, sapien enim suscipit felis, nec suscipit nisl lacus ac leo. 21 | 22 | Sed efficitur magna enim, placerat viverra nisi cursus at. Proin in nisl risus. Mauris posuere commodo commodo. 23 | Vestibulum at ipsum mauris. Quisque molestie, enim et dignissim varius, quam ipsum pretium risus, quis posuere est 24 | neque at mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec quis 25 | justo quis orci fermentum rutrum. Pellentesque malesuada diam at augue bibendum vulputate. Donec fermentum 26 | pellentesque dolor. Pellentesque consequat molestie augue, nec fringilla risus tristique quis. Nunc imperdiet 27 | nisl nec ipsum lacinia consectetur. Aenean id est sit amet ligula accumsan pharetra ut at enim. Ut diam sapien, 28 | vehicula a felis et, bibendum sollicitudin eros. Duis suscipit neque sed tortor bibendum, sit amet cursus massa 29 | consectetur. Cras neque ligula, molestie vitae fermentum quis, interdum nec dui. 30 | 31 | Duis mollis, lacus id scelerisque ultricies, risus nunc scelerisque libero, sed congue mauris ante ac metus. 32 | In in malesuada mauris, malesuada sollicitudin ex. Mauris maximus dui a arcu cursus, eget lobortis ex blandit. 33 | Maecenas blandit est lacinia nibh vestibulum porttitor. Cras mauris magna, eleifend eu maximus vitae, aliquam 34 | vitae risus. Vestibulum condimentum faucibus urna, vitae accumsan tortor sodales nec. Mauris placerat ultricies 35 | dui nec vehicula. Fusce ut nibh felis. 36 | 37 | Phasellus accumsan, dui quis congue interdum, dolor orci vulputate odio, sed sagittis nulla velit vitae orci. 38 | In pretium elit magna, et sodales ante suscipit et. Quisque facilisis tortor sem, eu venenatis orci cursus ut. 39 | Fusce facilisis sed leo et cursus. Cras mi nisl, imperdiet et fringilla eu, lacinia vitae justo. Etiam consequat 40 | sollicitudin risus, at mattis odio feugiat a. Proin pharetra, nibh ac maximus mollis, lectus felis interdum nibh, 41 | non tincidunt justo felis eu libero. Vivamus placerat libero sed ultricies dictum. Nunc molestie eu orci at ornare. 42 | Quisque sollicitudin vulputate purus, at scelerisque eros volutpat quis. 43 | -------------------------------------------------------------------------------- /tests/vs2015/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Basic preprocessor and lexer tests for Windows 3 | // 4 | 5 | #define LEXER_IMPLEMENTATION 6 | #include "../../lexer.hpp" 7 | 8 | #define PREPROCESSOR_IMPLEMENTATION 9 | #include "../../preprocessor.hpp" 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | preprocessor pp; 17 | 18 | // Global definitions: 19 | { 20 | const std::int64_t ival = 1337; 21 | const double dval = 3.141592; 22 | const std::string sval = "Who is John Galt?"; 23 | 24 | assert(pp.define("TEST_BUILTIN_INT", ival, false) == true); 25 | assert(pp.define("TEST_BUILTIN_FLT", dval, false) == true); 26 | assert(pp.define("TEST_BUILTIN_STR", sval, false) == true); 27 | 28 | assert(pp.is_defined("TEST_BUILTIN_INT") == true); 29 | assert(pp.is_defined("TEST_BUILTIN_FLT") == true); 30 | assert(pp.is_defined("TEST_BUILTIN_STR") == true); 31 | 32 | // A function-like macro: 33 | assert(pp.define("#define SQUARE(x) ((x) * (x))", false) == true); 34 | assert(pp.is_defined("SQUARE") == true); 35 | 36 | // Don't allow it to be redefined. 37 | assert(pp.define("#define SQUARE 2", false) == false); 38 | } 39 | 40 | // Check the values of the above definitions: 41 | { 42 | std::int64_t ival = 0; 43 | assert(pp.find_macro_value("TEST_BUILTIN_INT", &ival) == true); 44 | assert(ival == 1337); 45 | 46 | double dval = 0; 47 | assert(pp.find_macro_value("TEST_BUILTIN_FLT", &dval) == true); 48 | assert(dval == 3.141592); 49 | 50 | std::string sval; 51 | assert(pp.find_macro_value("TEST_BUILTIN_STR", &sval) == true); 52 | assert(sval == "Who is John Galt?"); 53 | 54 | int num_tokens = 0; 55 | const auto p_macro_tokens = pp.find_macro_tokens("SQUARE", &num_tokens); 56 | 57 | const char * tokens_expected[]{ "(", "(", "x", ")", "*", "(", "x", ")", ")" }; 58 | assert(static_cast(num_tokens) == (sizeof(tokens_expected) / sizeof(tokens_expected[0]))); 59 | 60 | for (int t = 0; t < num_tokens; ++t) 61 | { 62 | assert(p_macro_tokens[t] == tokens_expected[t]); 63 | } 64 | } 65 | 66 | // Expression evaluator: 67 | { 68 | std::int64_t iresult = 0; 69 | double dresult = 0; 70 | bool success; 71 | 72 | success = pp.eval("(1 + 1) * (1 + 1) == 4", &iresult, &dresult, false, false, false); 73 | assert(success == true); 74 | assert(iresult == 1); 75 | assert(dresult == 1.0); 76 | 77 | success = pp.eval("(1 << 1) ^ (1 << 2)", &iresult, &dresult, false, false, false); 78 | assert(success == true); 79 | assert(iresult == 6); 80 | assert(dresult == 6.0); 81 | 82 | success = pp.eval("1.5 + 3.0", &iresult, &dresult, false, false, false); 83 | assert(success == true); 84 | assert(iresult == 4); 85 | assert(dresult == 4.5); 86 | 87 | // We can also test if macros are defined in the preprocessor instance: 88 | success = pp.eval("defined(SQUARE)", &iresult, &dresult, false, false, false); 89 | assert(success == true); 90 | assert(iresult == 1); 91 | assert(dresult == 1); 92 | } 93 | 94 | // Built-in macros, scripts: 95 | { 96 | const char scr[] = "#define ONE 1\n" 97 | "#define TWO 2\n" 98 | "#define THREE 3\n" 99 | "#define FOUR 4\n"; 100 | pp.init_from_memory(scr, sizeof(scr) - 1, "test_script.txt", 101 | preprocessor::flags::no_fatal_errors); 102 | 103 | std::string result; 104 | if (!pp.preprocess(&result)) 105 | { 106 | std::cerr << "Failed to preprocess script!\n"; 107 | return EXIT_FAILURE; 108 | } 109 | 110 | assert(pp.is_defined("__FILE__")); 111 | assert(pp.is_defined("__LINE__")); 112 | assert(pp.is_defined("__DATE__")); 113 | assert(pp.is_defined("__TIME__")); 114 | assert(pp.is_defined("__VA_ARGS__")); 115 | 116 | const bool allow_built_ins = true; 117 | lexer::token file, line, date, time, args; 118 | assert(pp.find_macro_token("__FILE__", &file, allow_built_ins) == true); // current script loc 119 | assert(pp.find_macro_token("__LINE__", &line, allow_built_ins) == true); // current script loc 120 | assert(pp.find_macro_token("__DATE__", &date, allow_built_ins) == true); // current system date 121 | assert(pp.find_macro_token("__TIME__", &time, allow_built_ins) == true); // current system time 122 | assert(pp.find_macro_token("__VA_ARGS__", &args, allow_built_ins) == false); // can't expand in this context 123 | 124 | std::cout << "__FILE__: " << file.as_string() << '\n'; 125 | std::cout << "__LINE__: " << line.as_string() << '\n'; 126 | std::cout << "__DATE__: " << date.as_string() << '\n'; 127 | std::cout << "__TIME__: " << time.as_string() << '\n'; 128 | std::cout << "__VA_ARGS__: " << args.as_string() << '\n'; 129 | 130 | std::int64_t num = 0; 131 | assert(pp.find_macro_value("ONE", &num) && num == 1); 132 | assert(pp.find_macro_value("TWO", &num) && num == 2); 133 | assert(pp.find_macro_value("THREE", &num) && num == 3); 134 | assert(pp.find_macro_value("FOUR", &num) && num == 4); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /tests/preprocessor/test_misc.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: test_misc.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 25/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Testing some of the miscellaneous member methods of the preprocessor class. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -Wshadow -pedantic -I../../ test_misc.cpp -o pp_test_misc 13 | 14 | #define LEXER_IMPLEMENTATION 15 | #include "lexer.hpp" 16 | 17 | #define PREPROCESSOR_IMPLEMENTATION 18 | #include "preprocessor.hpp" 19 | 20 | #include 21 | #include 22 | 23 | int main() 24 | { 25 | preprocessor pp; 26 | 27 | // Global definitions: 28 | { 29 | const std::int64_t ival = 1337; 30 | const double dval = 3.141592; 31 | const std::string sval = "Who is John Galt?"; 32 | 33 | assert(pp.define("TEST_BUILTIN_INT", ival, false) == true); 34 | assert(pp.define("TEST_BUILTIN_FLT", dval, false) == true); 35 | assert(pp.define("TEST_BUILTIN_STR", sval, false) == true); 36 | 37 | assert(pp.is_defined("TEST_BUILTIN_INT") == true); 38 | assert(pp.is_defined("TEST_BUILTIN_FLT") == true); 39 | assert(pp.is_defined("TEST_BUILTIN_STR") == true); 40 | 41 | // A function-like macro: 42 | assert(pp.define("#define SQUARE(x) ((x) * (x))", false) == true); 43 | assert(pp.is_defined("SQUARE") == true); 44 | 45 | // Don't allow it to be redefined. 46 | assert(pp.define("#define SQUARE 2", false) == false); 47 | } 48 | 49 | // Check the values of the above definitions: 50 | { 51 | std::int64_t ival = 0; 52 | assert(pp.find_macro_value("TEST_BUILTIN_INT", &ival) == true); 53 | assert(ival == 1337); 54 | 55 | double dval = 0; 56 | assert(pp.find_macro_value("TEST_BUILTIN_FLT", &dval) == true); 57 | assert(dval == 3.141592); 58 | 59 | std::string sval; 60 | assert(pp.find_macro_value("TEST_BUILTIN_STR", &sval) == true); 61 | assert(sval == "Who is John Galt?"); 62 | 63 | int num_tokens = 0; 64 | const auto p_macro_tokens = pp.find_macro_tokens("SQUARE", &num_tokens); 65 | 66 | const char * tokens_expected[]{ "(", "(", "x", ")", "*", "(", "x", ")", ")" }; 67 | assert(static_cast(num_tokens) == (sizeof(tokens_expected) / sizeof(tokens_expected[0]))); 68 | 69 | for (int t = 0; t < num_tokens; ++t) 70 | { 71 | assert(p_macro_tokens[t] == tokens_expected[t]); 72 | } 73 | } 74 | 75 | // Expression evaluator: 76 | { 77 | std::int64_t iresult = 0; 78 | double dresult = 0; 79 | bool success; 80 | 81 | success = pp.eval("(1 + 1) * (1 + 1) == 4", &iresult, &dresult, false, false, false); 82 | assert(success == true); 83 | assert(iresult == 1); 84 | assert(dresult == 1.0); 85 | 86 | success = pp.eval("(1 << 1) ^ (1 << 2)", &iresult, &dresult, false, false, false); 87 | assert(success == true); 88 | assert(iresult == 6); 89 | assert(dresult == 6.0); 90 | 91 | success = pp.eval("1.5 + 3.0", &iresult, &dresult, false, false, false); 92 | assert(success == true); 93 | assert(iresult == 4); 94 | assert(dresult == 4.5); 95 | 96 | // We can also test if macros are defined in the preprocessor instance: 97 | success = pp.eval("defined(SQUARE)", &iresult, &dresult, false, false, false); 98 | assert(success == true); 99 | assert(iresult == 1); 100 | assert(dresult == 1); 101 | } 102 | 103 | // Built-in macros, scripts: 104 | { 105 | const char scr[] = "#define ONE 1\n" 106 | "#define TWO 2\n" 107 | "#define THREE 3\n" 108 | "#define FOUR 4\n"; 109 | pp.init_from_memory(scr, sizeof(scr) - 1, "test_script.txt", 110 | preprocessor::flags::no_fatal_errors); 111 | 112 | std::string result; 113 | if (!pp.preprocess(&result)) 114 | { 115 | std::cerr << "Failed to preprocess script!\n"; 116 | return EXIT_FAILURE; 117 | } 118 | 119 | assert(pp.is_defined("__FILE__")); 120 | assert(pp.is_defined("__LINE__")); 121 | assert(pp.is_defined("__DATE__")); 122 | assert(pp.is_defined("__TIME__")); 123 | assert(pp.is_defined("__VA_ARGS__")); 124 | 125 | const bool allow_built_ins = true; 126 | lexer::token file, line, date, time, args; 127 | assert(pp.find_macro_token("__FILE__", &file, allow_built_ins) == true); // current script loc 128 | assert(pp.find_macro_token("__LINE__", &line, allow_built_ins) == true); // current script loc 129 | assert(pp.find_macro_token("__DATE__", &date, allow_built_ins) == true); // current system date 130 | assert(pp.find_macro_token("__TIME__", &time, allow_built_ins) == true); // current system time 131 | assert(pp.find_macro_token("__VA_ARGS__", &args, allow_built_ins) == false); // can't expand in this context 132 | 133 | std::cout << "__FILE__: " << file.as_string() << '\n'; 134 | std::cout << "__LINE__: " << line.as_string() << '\n'; 135 | std::cout << "__DATE__: " << date.as_string() << '\n'; 136 | std::cout << "__TIME__: " << time.as_string() << '\n'; 137 | std::cout << "__VA_ARGS__: " << args.as_string() << '\n'; 138 | 139 | std::int64_t num = 0; 140 | assert(pp.find_macro_value("ONE", &num) && num == 1); 141 | assert(pp.find_macro_value("TWO", &num) && num == 2); 142 | assert(pp.find_macro_value("THREE", &num) && num == 3); 143 | assert(pp.find_macro_value("FOUR", &num) && num == 4); 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /tests/lexer/simple_ini_parser.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: simple_ini_parser.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 09/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Basic sample demonstrating how to use the lexer class to parse INI configuration files. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -pedantic -I../../ -o simple_ini_parser simple_ini_parser.cpp 13 | 14 | #define LEXER_ERROR_WARN_USE_ANSI_COLOR_CODES 15 | #define LEXER_IMPLEMENTATION 16 | #include "lexer.hpp" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace ini 24 | { 25 | 26 | enum class value_type 27 | { 28 | none, 29 | string, 30 | number, 31 | ip_addr, 32 | boolean 33 | }; 34 | 35 | // This could also be a tagged union but since std::string has 36 | // constructor/destructor we use a struct to keep things simple. 37 | struct value_holder final 38 | { 39 | std::string string_val; 40 | double number_val = 0; 41 | std::uint64_t ip_addr_val = 0; 42 | bool bool_val = false; 43 | value_type type = value_type::none; 44 | }; 45 | 46 | // [key, value] pairs. 47 | using section = std::unordered_map; 48 | 49 | // List of sections indexed by name. 50 | using sections_list = std::unordered_map; 51 | 52 | // Parses the .ini file filling the sections_list. 53 | // Might throw lexer::exceptions on error. 54 | void parse_file(std::string filename, sections_list * out_sections) 55 | { 56 | lexer lex; 57 | lexer::token tok; 58 | section * current_section = nullptr; 59 | 60 | lex.init_from_file(std::move(filename), lexer::flags::allow_ip_addresses); 61 | 62 | while (lex.next_token(&tok)) 63 | { 64 | if (tok.is_punctuation()) 65 | { 66 | if (tok == '#' || tok == ';') 67 | { 68 | // ini-style comments. 69 | // The lexer only skips C and C++ style comments by default. 70 | lex.skip_rest_of_line(); 71 | } 72 | else if (tok == '[') // New section 73 | { 74 | lex.expect_token_type(lexer::token::type::identifier, 0, &tok); 75 | lex.expect_token_char(']'); // Closing the section 76 | 77 | current_section = &(*out_sections)[tok.as_string()]; 78 | } 79 | } 80 | else if (tok.is_identifier()) // Key=value pair 81 | { 82 | value_holder ini_val; 83 | lexer::token value_tok; 84 | 85 | lex.expect_token_char('='); 86 | lex.expect_any_token(&value_tok); 87 | 88 | if (value_tok.is_boolean() || value_tok.is_number() || value_tok.is_string()) 89 | { 90 | if (current_section == nullptr) // Make a default if none was set yet. 91 | { 92 | current_section = &(*out_sections)["global"]; 93 | } 94 | 95 | if (value_tok.is_string()) 96 | { 97 | ini_val.type = value_type::string; 98 | ini_val.string_val = value_tok.as_string(); 99 | } 100 | else if (value_tok.is_number()) 101 | { 102 | if (value_tok.get_flags() & lexer::token::flags::ip_address) 103 | { 104 | ini_val.type = value_type::ip_addr; 105 | ini_val.ip_addr_val = value_tok.as_uint64(); 106 | } 107 | else 108 | { 109 | ini_val.type = value_type::number; 110 | ini_val.number_val = value_tok.as_double(); 111 | } 112 | } 113 | else if (value_tok.is_boolean()) 114 | { 115 | ini_val.type = value_type::boolean; 116 | ini_val.bool_val = value_tok.as_bool(); 117 | } 118 | 119 | (*current_section)[tok.as_string()] = ini_val; 120 | } 121 | } 122 | } 123 | } 124 | 125 | // Debug printing of a sections_list built by parse_file(). 126 | void print_sections_list(const sections_list & sections, std::ostream * out_str) 127 | { 128 | for (const auto & sect : sections) 129 | { 130 | (*out_str) << "[" << sect.first << "]\n"; 131 | 132 | for (const auto & var : sect.second) 133 | { 134 | (*out_str) << var.first << " = "; 135 | 136 | switch (var.second.type) 137 | { 138 | case value_type::string : 139 | (*out_str) << "\"" << var.second.string_val << "\""; 140 | break; 141 | 142 | case value_type::number : 143 | (*out_str) << var.second.number_val; 144 | break; 145 | 146 | case value_type::ip_addr : 147 | (*out_str) << ((var.second.ip_addr_val & 0xFF000000) >> 24) << "." 148 | << ((var.second.ip_addr_val & 0x00FF0000) >> 16) << "." 149 | << ((var.second.ip_addr_val & 0x0000FF00) >> 8) << "." 150 | << ((var.second.ip_addr_val & 0x000000FF) >> 0) << ":" 151 | << (var.second.ip_addr_val >> 32); // Port # follows in the leftmost 32 bits. 152 | break; 153 | 154 | case value_type::boolean : 155 | (*out_str) << (var.second.bool_val ? "true" : "false"); 156 | break; 157 | 158 | default : 159 | (*out_str) << "none"; 160 | break; 161 | } // switch (var.second.type) 162 | 163 | (*out_str) << "\n"; 164 | } 165 | 166 | (*out_str) << "\n"; 167 | } 168 | } 169 | 170 | } // namespace ini {} 171 | 172 | // ======================================================== 173 | // main(): 174 | // ======================================================== 175 | 176 | int main() 177 | { 178 | ini::sections_list sections; 179 | ini::parse_file("lex_test_5.ini", §ions); 180 | 181 | std::cout << "\nContents of the parsed INI file:\n\n"; 182 | ini::print_sections_list(sections, &std::cout); 183 | } 184 | -------------------------------------------------------------------------------- /tests/vs2015/tests_vs2015.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {A7270887-0A29-4B3E-BAE0-F039873ACE09} 23 | Win32Proj 24 | tests_vs2015 25 | 8.1 26 | 27 | 28 | 29 | Application 30 | true 31 | v140 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v140 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v140 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v140 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | 87 | 88 | Level4 89 | Disabled 90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | 100 | 101 | Level4 102 | Disabled 103 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 104 | 105 | 106 | Console 107 | true 108 | 109 | 110 | 111 | 112 | Level4 113 | 114 | 115 | MaxSpeed 116 | true 117 | true 118 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 119 | 120 | 121 | Console 122 | true 123 | true 124 | true 125 | 126 | 127 | 128 | 129 | Level4 130 | 131 | 132 | MaxSpeed 133 | true 134 | true 135 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 136 | 137 | 138 | Console 139 | true 140 | true 141 | true 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /tests/preprocessor/test_files/evals_and_macros.h: -------------------------------------------------------------------------------- 1 | 2 | // ------------------------------------ 3 | // $evalfloat / $evalint: 4 | // ------------------------------------ 5 | 6 | // result = 11.0 7 | $evalfloat((1 + 2) + 3 + 4 + 1); 8 | $evalfloat(1 + 2 + 3 + 4 + 1); 9 | 10 | // result = 10.0 11 | $evalfloat(1 + 2 + (3 + 4)); 12 | $evalfloat(1 + 2 + 3 + 4); 13 | 14 | // result = 10.0 15 | $evalfloat(1 + (2 + 3) + 4); 16 | $evalfloat(1 + 2 + 3 + 4); 17 | 18 | // result = 10 19 | $evalint((1 + 2) + (3 + 4)); 20 | $evalint(1 + 2 + 3 + 4); 21 | 22 | // result = 15 23 | $evalint(1 + (1 + 2) + (3 + 4) + 2 + (1 + 1)); 24 | $evalint(1 + 1 + 2 + 3 + 4 + 2 + 1 + 1); 25 | 26 | // result = 14 27 | $evalint(1 + (2 + (2 + 2) + 3) + 4); 28 | $evalint(1 + 2 + 2 + 2 + 3 + 4); 29 | 30 | // result = 18 31 | $evalint(1 + (2 + (2 + 2) + (2 + 2) + 3) + 4); 32 | $evalint(1 + 2 + 2 + 2 + 2 + 2 + 3 + 4); 33 | 34 | // result = 16 35 | $evalint(1 + (1 + (1 + (1 + 1))) + 1 + 2 + (4 + 4)); 36 | 37 | // result = 8 38 | $evalint((1 + (1 + (1 + (1 + 1))) + 1 + 2 + (4 + 4)) * 0.5); 39 | 40 | // ------------------------------------ 41 | // $eval: 42 | // ------------------------------------ 43 | 44 | $eval(1 + 1) // 2 45 | $eval(1.5 * 2) // 3.0 46 | $eval(3.0 / 2.0) // 1.5 47 | $eval(-0); // 0 48 | $eval(+0); // 0 49 | $eval(-1); // -1 50 | $eval(+1); // +1 51 | $eval(-1 + (-2 + 3)); // 0 52 | $eval(+666); // 666 53 | $eval(0 + !1); // 0 54 | $eval(0 + ~1); // -2 55 | $eval(0 + !!1); // 1 56 | $eval(0 + ~!1); // -1 57 | $eval(0 + - -1); // 1 58 | $eval(0 + -(-1)); // 1 59 | $eval(0 + - + 1); // -1 60 | $eval(~ -1); // 0 61 | $eval(! -1); // 0 62 | $eval(~(-1)); // 0 63 | $eval(!(-1)); // 0 64 | $eval(- ~1); // 2 65 | $eval(- !1); // 0 66 | $eval(-(~1)); // 2 67 | $eval(-(!1)); // 0 68 | $eval(2 + - ~1); // 4 69 | $eval(2 + - !1); // 2 70 | $eval(! -0); // 1 71 | $eval(- !0); // -1 72 | $eval(!(-0)); // 1 73 | $eval(-(!0)); // -1 74 | $eval(-42); // -42 75 | $eval(-(2 + 1)); // -3 76 | $eval(! -0.0); // 1.0 77 | $eval(- !0.0); // -1.0 78 | $eval(! -0.5); // 0.0 79 | $eval(- !0.5); // -0.0 80 | $eval(1 + 2 - 3 + 4); // 4 81 | $eval(1 + 2 * 3 + 4); // 11 82 | $eval((1 ? 42 : 666) + 1); // 43 83 | $eval((0 ? 42 : 666) + 1); // 667 84 | $eval(-1 - -2 + 4); // 5 85 | $eval(-1 - (-2 + 4)); // -3 86 | $eval(((1 < 2) ? -1.0 : -2.0)); // -1.0 87 | $eval(4 != 5); // 1 88 | $eval(1 - 2 * 2); // -3 89 | $eval(1 + -(2 * 2)); // -3 90 | $eval(-1 - -(-2 + 4)); // 1 91 | $eval(1 + -(1 + 1)); // -1 92 | 93 | // Predefined math constants visible inside $eval(): 94 | $eval(PI); 95 | $eval(E); 96 | $eval(TAU); 97 | $eval(INV_TAU); 98 | $eval(HALF_PI); 99 | $eval(INV_PI); 100 | $eval(DEG2RAD); 101 | $eval(RAD2DEG); 102 | 103 | // Some of the built-in math functions: 104 | $eval(2 * cos(3 + 2)); 105 | $eval(round(-1.4)); 106 | $eval(floor(PI)); 107 | $eval(ceil(PI)); 108 | $eval(abs(+2)); 109 | $eval(abs(-2)); 110 | $eval(1 + acos(cos(1)) - 1); 111 | $eval(log2(exp2(1.0))) 112 | 113 | // ------------------------------------ 114 | // #warn/#warning command: 115 | // ------------------------------------ 116 | 117 | #warn // no warning message 118 | 119 | #warn "Single line warning message." 120 | 121 | #warning "A long, " \ 122 | "multi-line warning " \ 123 | "message with several words." 124 | 125 | "Some unrelated string"; 126 | 127 | #pragma(warning: disable) 128 | #warning "This warning will NOT print." 129 | 130 | #pragma(warning: enable) 131 | #warning "This warning will print." 132 | 133 | // ------------------------------------ 134 | // #define macros: 135 | // ------------------------------------ 136 | 137 | #define HELLO 111 222 333 "hello!" \ 138 | 555 "next up" \ 139 | "Howdy " 3.14 140 | 141 | #define WORLD 222 142 | #define WORLD 666 /* redefinition warning */ 143 | 144 | #define FANCY_MACRO \ 145 | do \ 146 | { \ 147 | for (int i = 0; i < 42; ++i) \ 148 | { \ 149 | std::printf("i = %i", i); \ 150 | } \ 151 | } while (0) 152 | 153 | #define EMPTY_MACRO 154 | #define GET_DATE __DATE__ 155 | #define GET_LINE __LINE__ 156 | 157 | #define META_FUNC(x, y, z) { (z + y + x) - [x * x * x] + { y, y } / | z ^ z | } 158 | 159 | // Expand them: 160 | HELLO; 161 | WORLD; 162 | FANCY_MACRO; 163 | EMPTY_MACRO; 164 | GET_DATE; 165 | GET_LINE; 166 | META_FUNC(1, 2, 3); 167 | 168 | #define MY_ASSERT(expr) if (!(expr)) { print_error(#expr); } 169 | 170 | MY_ASSERT(1 < 2); 171 | MY_ASSERT(true != false); 172 | 173 | #define TEST_STRINGIZE(bar) "foo-" #bar "-baz" 174 | #define TEST_CONCAT_1(bar) foo_ ## bar 175 | #define TEST_CONCAT_2(bar) foo_ ## bar ## _baz 176 | 177 | TEST_STRINGIZE(hello); 178 | TEST_STRINGIZE(" hello \"john\" "); 179 | 180 | TEST_CONCAT_1(world); 181 | TEST_CONCAT_2(world); 182 | 183 | #define FUNC_LIKE_MACRO(a, b, c) \ 184 | do \ 185 | { \ 186 | std::printf("%d\n", a); \ 187 | std::printf("%f\n", b##__LINE__); \ 188 | std::printf("%s\n", #c); \ 189 | std::printf("%s\n", __FILE__); \ 190 | META_FUNC(1, 2, c); \ 191 | std::printf("FINISHED!\n"); \ 192 | } while (0) 193 | 194 | FUNC_LIKE_MACRO(1, A_, 33); 195 | 196 | #define STRING_JOIN3_HELPER(x, y, z) x ## y ## z 197 | #define STRING_JOIN3(x, y, z) STRING_JOIN3_HELPER(x, y, z) 198 | 199 | STRING_JOIN3_HELPER(Foo_, Bar_, Baz1); 200 | STRING_JOIN3(Foo_, Bar_, Baz2); 201 | 202 | #define VARIADIC_MACRO_0(...) \ 203 | do \ 204 | { \ 205 | std::printf(__VA_ARGS__); \ 206 | } while (0) 207 | 208 | #define VARIADIC_MACRO_1(x, ...) \ 209 | do \ 210 | { \ 211 | std::printf("x = %s\n", #x); \ 212 | std::printf(__VA_ARGS__); \ 213 | } while (0) 214 | 215 | #define VARIADIC_MACRO_2(x, y, ...) \ 216 | do \ 217 | { \ 218 | std::printf("x = %s\n", #x); \ 219 | std::printf("y = %s\n", #y); \ 220 | std::printf(__VA_ARGS__); \ 221 | } while (0) 222 | 223 | VARIADIC_MACRO_0(1, 2, 3, "foo", "bar", some_var0); 224 | VARIADIC_MACRO_1(AAA, 1, 2, 3, "foo", "bar", some_var1); 225 | VARIADIC_MACRO_2(AAA, BBB, 1, 2, 3, "foo", "bar", some_var2); 226 | 227 | #define PR3(a, b, c) std::printf("%s, %s, %s\n", a, b, c) 228 | #define PRINT3(...) PR3(__VA_ARGS__) 229 | PRINT3("one", "two", "three"); 230 | 231 | // ------------------------------------ 232 | // Preprocessor conditional directives: 233 | // ------------------------------------ 234 | 235 | //#define FOO 236 | //#define BAR 237 | 238 | #ifdef FOO 239 | << FOO is defined >> 240 | #ifndef BAR 241 | << Top BAR is NOT defined >> 242 | #endif // BAR 243 | #else // FOO 244 | << FOO not defined >> 245 | #ifndef BAR 246 | << Botton BAR is NOT defined >> 247 | #endif // BAR 248 | #endif // FOO 249 | 250 | #if ((((0 || 1) * 0) == 0) && 1 < 2) 251 | << 1 >> // This one should remain 252 | #elif 1 253 | << 2 >> // This one is stripped 254 | #endif 255 | 256 | //#define UNDEFINED_M 1 257 | //#define FOO 1 258 | //#define BAR 1 259 | 260 | #if defined(UNDEFINED_M) // same as 0 if not defined 261 | [[IF]] 262 | #elif FOO 263 | [[ELIF_1]] 264 | #elif BAR 265 | [[ELIF_2]] 266 | #else 267 | [[ELSE]] 268 | #endif 269 | 270 | -------------------------------------------------------------------------------- /tests/lexer/simple_cmdline_parser.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: simple_cmdline_parser.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 09/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Basic sample demonstrating how to use the lexer class to implement a command-line parser. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -pedantic -I../../ -o simple_cmdline_parser simple_cmdline_parser.cpp 13 | 14 | #define LEXER_ERROR_WARN_USE_ANSI_COLOR_CODES 15 | #define LEXER_IMPLEMENTATION 16 | #include "lexer.hpp" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace cmdline 25 | { 26 | 27 | // 28 | // Accepted types of command-line flags: 29 | // -x : boolean flag, true if present, false otherwise. 30 | // --foo : long version of the boolean flag. 31 | // --foo=bar : long flag with an explicit value. 32 | // 33 | // The long flags can contain one or more '-' in the middle: 34 | // --foo-bar=baz 35 | // 36 | // The value can be quoted to denote a string with whitespace: 37 | // --foo="hello world" 38 | // 39 | 40 | enum class value_type 41 | { 42 | none, 43 | string, 44 | number, 45 | ip_addr 46 | }; 47 | 48 | // This could also be a tagged union but since std::string has 49 | // constructor/destructor we use a struct to keep things simple. 50 | struct value_holder final 51 | { 52 | std::string string_val; 53 | double number_val = 0; 54 | std::uint64_t ip_addr_val = 0; 55 | value_type type = value_type::none; 56 | }; 57 | 58 | // [flag-name, value] pairs. If the flag has no value the type tag is set to value_type::none. 59 | using flag_pair = std::pair; 60 | using flags_list = std::unordered_map; 61 | 62 | // Test if the given flag is defined, regardless of its value. 63 | bool has_flag(const flags_list & flags, const std::string & flag_wanted) 64 | { 65 | return flags.find(flag_wanted) != std::end(flags); 66 | } 67 | 68 | // Parses the traditional main-style command line using a lexer. 69 | // Might throw lexer::exceptions on error. 70 | void parse_argc_argv(const int argc, const char * argv[], flags_list * out_flags) 71 | { 72 | lexer lex; 73 | lexer::token tok; 74 | 75 | // Each entry of the argv[] is a string, skipping the first 76 | // which is always the program name, so we lex each individual 77 | // command string to make things simpler. 78 | for (int i = 1; i < argc; ++i) 79 | { 80 | lex.init_from_memory(argv[i], std::strlen(argv[i]), "(cmdline)", 81 | lexer::flags::allow_number_names | 82 | lexer::flags::allow_ip_addresses | 83 | lexer::flags::allow_multi_char_literals); 84 | 85 | while (lex.next_token(&tok)) 86 | { 87 | if (tok == '-') 88 | { 89 | lex.expect_token_type(lexer::token::type::identifier, 0, &tok); 90 | (*out_flags)[tok.as_string()] = {}; 91 | } 92 | else if (tok == "--") 93 | { 94 | lex.expect_token_type(lexer::token::type::identifier, 0, &tok); 95 | std::string flag_name = tok.as_string(); 96 | 97 | lexer::token next_tok; 98 | if (lex.next_token(&next_tok)) 99 | { 100 | if (next_tok == '-') // Composite name, like "foo-bar" 101 | { 102 | lex.expect_token_type(lexer::token::type::identifier, 0, &next_tok); 103 | 104 | flag_name += "-"; 105 | flag_name += next_tok.as_string(); 106 | (*out_flags)[flag_name] = {}; 107 | } 108 | else if (next_tok == '=') // Value part follows 109 | { 110 | if (!lex.next_token(&next_tok)) 111 | { 112 | lex.error("expected value after \'" + flag_name + "=\' in cmdline!"); 113 | } 114 | 115 | value_holder & current_flag = (*out_flags)[flag_name]; 116 | 117 | if (next_tok.is_string()) 118 | { 119 | current_flag.type = value_type::string; 120 | current_flag.string_val = next_tok.as_string(); 121 | } 122 | else if (next_tok.is_number()) 123 | { 124 | if (next_tok.get_flags() & lexer::token::flags::ip_address) 125 | { 126 | current_flag.type = value_type::ip_addr; 127 | current_flag.ip_addr_val = next_tok.as_uint64(); 128 | } 129 | else // Integer or float number. 130 | { 131 | current_flag.type = value_type::number; 132 | current_flag.number_val = next_tok.as_double(); 133 | } 134 | } 135 | else 136 | { 137 | lex.error("cmdline flag type is unsupported!"); 138 | } 139 | } 140 | else 141 | { 142 | lex.error("unexpected token in cmdline: \'" + next_tok.as_string() + "\"."); 143 | } 144 | } 145 | else 146 | { 147 | (*out_flags)[flag_name] = {}; 148 | } 149 | } 150 | } 151 | 152 | lex.clear(); 153 | } 154 | } 155 | 156 | // Debug printing of a flags_list built by parse_argc_argv(). 157 | void print_flags_list(const flags_list & flags, std::ostream * out_str) 158 | { 159 | for (const auto & f : flags) 160 | { 161 | (*out_str) << "FLAG: " << f.first; 162 | 163 | if (f.second.type != value_type::none) 164 | { 165 | (*out_str) << "="; 166 | 167 | switch (f.second.type) 168 | { 169 | case value_type::string : 170 | (*out_str) << "\"" << f.second.string_val << "\""; 171 | break; 172 | 173 | case value_type::number : 174 | (*out_str) << f.second.number_val; 175 | break; 176 | 177 | case value_type::ip_addr : 178 | (*out_str) << ((f.second.ip_addr_val & 0xFF000000) >> 24) << "." 179 | << ((f.second.ip_addr_val & 0x00FF0000) >> 16) << "." 180 | << ((f.second.ip_addr_val & 0x0000FF00) >> 8) << "." 181 | << ((f.second.ip_addr_val & 0x000000FF) >> 0) << ":" 182 | << (f.second.ip_addr_val >> 32); // Port # follows in the leftmost 32 bits. 183 | break; 184 | 185 | default : 186 | break; 187 | } // switch (f.second.type) 188 | } 189 | 190 | (*out_str) << "\n"; 191 | } 192 | 193 | (*out_str) << "\n"; 194 | } 195 | 196 | } // namespace cmdline {} 197 | 198 | // ======================================================== 199 | // main(): 200 | // ======================================================== 201 | 202 | int main(int argc, const char * argv[]) 203 | { 204 | // Sample command line: 205 | // 206 | // ./simple_cmdline_parser -x --foo1 --foo2-bar --foo3=42 --xyz='"hello world"' --ip=172.16.254.1:8080 --file='"some/file/path.txt"' -1z 207 | // 208 | 209 | cmdline::flags_list cmd_flags; 210 | cmdline::parse_argc_argv(argc, argv, &cmd_flags); 211 | 212 | std::cout << "\nFlags parsed from the command-line:\n\n"; 213 | cmdline::print_flags_list(cmd_flags, &std::cout); 214 | 215 | assert(cmdline::has_flag(cmd_flags, "x")); 216 | assert(cmdline::has_flag(cmd_flags, "foo1")); 217 | assert(cmdline::has_flag(cmd_flags, "foo2-bar")); 218 | assert(cmdline::has_flag(cmd_flags, "foo3")); 219 | assert(cmdline::has_flag(cmd_flags, "xyz")); 220 | assert(cmdline::has_flag(cmd_flags, "ip")); 221 | assert(cmdline::has_flag(cmd_flags, "file")); 222 | assert(cmdline::has_flag(cmd_flags, "1z")); 223 | } 224 | -------------------------------------------------------------------------------- /tests/lexer/misc_lex_tests.cpp: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================ 3 | // -*- C++ -*- 4 | // File: misc_lex_tests.cpp 5 | // Author: Guilherme R. Lampert 6 | // Created on: 09/05/16 7 | // License: GNU GPL v3. 8 | // Brief: Miscellaneous tests for the lexer class. 9 | // ================================================================================================ 10 | 11 | // Compiles with: 12 | // c++ -std=c++11 -Wall -Wextra -Weffc++ -pedantic -I../../ -o misc_lex_tests misc_lex_tests.cpp 13 | 14 | #define LEXER_ERROR_WARN_USE_ANSI_COLOR_CODES 15 | #define LEXER_IMPLEMENTATION 16 | #include "lexer.hpp" 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | // Verbose unless specified otherwise. 23 | #ifndef LEX_TESTS_VERBOSE 24 | #define LEX_TESTS_VERBOSE 1 25 | #endif // LEX_TESTS_VERBOSE 26 | 27 | #if LEX_TESTS_VERBOSE 28 | static void print_token(const lexer::token & tok) 29 | { 30 | std::string type_str = lexer::token::type_string(tok.get_type()); 31 | std::string flags_str = lexer::token::flags_string(tok.get_flags(), tok.is_punctuation()); 32 | 33 | if (flags_str.empty()) 34 | { 35 | flags_str = "0"; 36 | } 37 | 38 | std::cout << "\"" << tok.as_string() << "\" => " << "(" << type_str << ", " << flags_str << ")\n"; 39 | } 40 | #endif // LEX_TESTS_VERBOSE 41 | 42 | static bool floats_equivalent(const double a, const double b, const double epsilon = 0.0001) 43 | { 44 | return std::fabs(a - b) < epsilon; 45 | } 46 | 47 | static void lex_test_scan_num_and_string_values() 48 | { 49 | #if LEX_TESTS_VERBOSE 50 | std::cout << "\nScanning values as numbers and strings...\n"; 51 | #endif // LEX_TESTS_VERBOSE 52 | 53 | // no_string_concat so neighboring strings on separate lines are not merged. 54 | lexer lex{ "lex_test_1.txt", lexer::flags::no_string_concat | lexer::flags::allow_multi_char_literals }; 55 | 56 | std::string str; 57 | str = lex.scan_string(); assert(str == "x"); 58 | str = lex.scan_string(); assert(str == "4cc"); 59 | str = lex.scan_string(); assert(str == "hello world"); 60 | 61 | bool b; 62 | b = lex.scan_number(); assert(b == false); 63 | b = lex.scan_number(); assert(b == true); 64 | b = lex.scan_number(); assert(b == false); 65 | b = lex.scan_number(); assert(b == true); 66 | 67 | const char c = lex.scan_number(); assert(c == -120); 68 | const short s = lex.scan_number(); assert(s == -31000); 69 | const int i = lex.scan_number(); assert(i == -4096); 70 | const long l = lex.scan_number(); assert(l == 0x11AABBCC); 71 | 72 | const unsigned int ui = lex.scan_number(); assert(ui == 6789UL); 73 | const unsigned long ul = lex.scan_number(); assert(ul == 0x9908B0DFul); 74 | 75 | const std::uint8_t u8 = lex.scan_number(); assert(u8 == 0x12); 76 | const std::uint16_t u16 = lex.scan_number(); assert(u16 == 0xEEFF); 77 | const std::uint32_t u32 = lex.scan_number(); assert(u32 == 0xDEADBEEF); 78 | const std::uint64_t u64 = lex.scan_number(); assert(u64 == 0xCAFED00DCAFEBABE); 79 | 80 | const float f = lex.scan_number(); assert(floats_equivalent(f, 2.718281828f)); 81 | const double d = lex.scan_number(); assert(floats_equivalent(d, 3.14159265358979323846L)); 82 | 83 | // Another pass just for printing: 84 | #if LEX_TESTS_VERBOSE 85 | lexer::token tok; 86 | lex.reset(); 87 | while (lex.next_token(&tok)) 88 | { 89 | print_token(tok); 90 | } 91 | #endif // LEX_TESTS_VERBOSE 92 | } 93 | 94 | static void lex_test_scan_matrices() 95 | { 96 | #if LEX_TESTS_VERBOSE 97 | std::cout << "\nScanning matrices of floats...\n"; 98 | #endif // LEX_TESTS_VERBOSE 99 | 100 | lexer lex{ "lex_test_4.txt" }; 101 | 102 | // 1D matrix: 103 | { 104 | double mat_1d[10] = {0}; 105 | lex.scan_matrix1d(10, mat_1d, "[", "]"); 106 | 107 | // 10 values for 9 to 0: 108 | double expected_val = 9.0; 109 | for (int i = 0; i < 10; ++i) 110 | { 111 | assert(mat_1d[i] == expected_val); 112 | expected_val -= 1.0; 113 | } 114 | } 115 | 116 | // 2D matrix: 117 | { 118 | double mat_2d[3 * 3] = {0}; 119 | lex.scan_matrix2d(3, 3, mat_2d, "{", "}"); 120 | 121 | // 9 values from 1 to 9: 122 | double expected_val = 1.0; 123 | for (int i = 0; i < 3 * 3; ++i) 124 | { 125 | assert(mat_2d[i] == expected_val); 126 | expected_val += 1.0; 127 | } 128 | } 129 | 130 | // 3D matrix: 131 | { 132 | int mat_3d[2 * 2 * 3] = {0}; 133 | lex.scan_matrix3d(2, 2, 3, mat_3d, "(", ")"); 134 | 135 | // 12 values from 1 to 12: 136 | int expected_val = 1; 137 | for (int i = 0; i < 2 * 2 * 3; ++i) 138 | { 139 | assert(mat_3d[i] == expected_val); 140 | expected_val += 1; 141 | } 142 | } 143 | 144 | #if LEX_TESTS_VERBOSE 145 | std::cout << "Values matched the expected.\n"; 146 | #endif // LEX_TESTS_VERBOSE 147 | } 148 | 149 | static void lex_test_scan_punctuations() 150 | { 151 | #if LEX_TESTS_VERBOSE 152 | std::cout << "\nScanning punctuations...\n"; 153 | #endif // LEX_TESTS_VERBOSE 154 | 155 | // The incoming file should have one instance of each punctuation. 156 | lexer lex{ "lex_test_2.txt" }; 157 | lexer::token tok; 158 | 159 | // Skip the first entry (punctuation_id::none) 160 | for (std::size_t i = 1; i < lexer::default_punctuations_size; ++i) 161 | { 162 | lex.expect_token_type(lexer::token::type::punctuation, 163 | static_cast(lexer::default_punctuations[i].id), 164 | &tok); 165 | 166 | #if LEX_TESTS_VERBOSE 167 | print_token(tok); 168 | #endif // LEX_TESTS_VERBOSE 169 | } 170 | } 171 | 172 | static void lex_test_custom_punct_table() 173 | { 174 | #if LEX_TESTS_VERBOSE 175 | std::cout << "\nTesting a custom punctuation table...\n"; 176 | #endif // LEX_TESTS_VERBOSE 177 | 178 | // Testing a "verbose" punctuation set. 179 | // Note: Order is strict and must match the lexer::punctuation_id enumeration. 180 | static const lexer::punctuation_def custom_punctuations[] 181 | { 182 | { nullptr, lexer::punctuation_id::none }, 183 | { "", lexer::punctuation_id::assign }, 184 | { "", lexer::punctuation_id::add }, 185 | { "", lexer::punctuation_id::sub }, 186 | { "", lexer::punctuation_id::mul }, 187 | { "
", lexer::punctuation_id::div }, 188 | { "", lexer::punctuation_id::mod }, 189 | { "", lexer::punctuation_id::rshift }, 190 | { "", lexer::punctuation_id::lshift }, 191 | { "", lexer::punctuation_id::add_assign }, 192 | { "", lexer::punctuation_id::sub_assign }, 193 | { "", lexer::punctuation_id::mul_assign }, 194 | { "", lexer::punctuation_id::div_assign }, 195 | { "", lexer::punctuation_id::mod_assign }, 196 | { "", lexer::punctuation_id::rshift_assign }, 197 | { "", lexer::punctuation_id::lshift_assign }, 198 | { "", lexer::punctuation_id::logic_and }, 199 | { "", lexer::punctuation_id::logic_or }, 200 | { "", lexer::punctuation_id::logic_not }, 201 | { "", lexer::punctuation_id::logic_eq }, 202 | { "", lexer::punctuation_id::logic_not_eq }, 203 | { "", lexer::punctuation_id::logic_greater }, 204 | { "", lexer::punctuation_id::logic_less }, 205 | { "", lexer::punctuation_id::logic_greater_eq }, 206 | { "", lexer::punctuation_id::logic_less_eq }, 207 | { "", lexer::punctuation_id::plus_plus }, 208 | { "", lexer::punctuation_id::minus_minus }, 209 | { "", lexer::punctuation_id::bitwise_and }, 210 | { "", lexer::punctuation_id::bitwise_or }, 211 | { "", lexer::punctuation_id::bitwise_xor }, 212 | { "", lexer::punctuation_id::bitwise_not }, 213 | { "", lexer::punctuation_id::bitwise_and_assign }, 214 | { "", lexer::punctuation_id::bitwise_or_assign }, 215 | { "", lexer::punctuation_id::bitwise_xor_assign }, 216 | { "", lexer::punctuation_id::dot }, 217 | { "", lexer::punctuation_id::arrow }, 218 | { "", lexer::punctuation_id::colon_colon }, 219 | { "", lexer::punctuation_id::dot_star }, 220 | { "", lexer::punctuation_id::comma }, 221 | { "", lexer::punctuation_id::semicolon }, 222 | { "", lexer::punctuation_id::colon }, 223 | { "", lexer::punctuation_id::question_mark }, 224 | { "", lexer::punctuation_id::ellipsis }, 225 | { "", lexer::punctuation_id::backslash }, 226 | { "", lexer::punctuation_id::open_parentheses }, 227 | { "", lexer::punctuation_id::close_parentheses }, 228 | { "", lexer::punctuation_id::open_bracket }, 229 | { "", lexer::punctuation_id::close_bracket }, 230 | { "", lexer::punctuation_id::open_curly_bracket }, 231 | { "", lexer::punctuation_id::close_curly_bracket }, 232 | { "", lexer::punctuation_id::preprocessor }, 233 | { "", lexer::punctuation_id::preprocessor_merge }, 234 | { "", lexer::punctuation_id::dollar_sign } 235 | }; 236 | 237 | // The additional table buffers must remain valid until the next set_punctuation_tables() or 238 | // set_default_punctuation_tables() call, so the best way to go about it is to provide pointers to static arrays. 239 | constexpr std::size_t custom_punctuations_size = sizeof(custom_punctuations) / sizeof(custom_punctuations[0]); 240 | static lexer::punct_table_index_type custom_punctuations_next[custom_punctuations_size]; 241 | static lexer::punct_table_index_type custom_punctuations_table[256]; 242 | 243 | // Now all lexer instances will share this punctuation set until a new one is set. 244 | lexer::set_punctuation_tables(custom_punctuations, 245 | custom_punctuations_table, 246 | custom_punctuations_next, 247 | custom_punctuations_size); 248 | 249 | lexer lex{ "lex_test_6.txt" }; 250 | lexer::token tok; 251 | 252 | // Skip the first entry (punctuation_id::none) 253 | for (std::size_t i = 1; i < custom_punctuations_size; ++i) 254 | { 255 | lex.expect_token_type(lexer::token::type::punctuation, 256 | static_cast(custom_punctuations[i].id), 257 | &tok); 258 | 259 | #if LEX_TESTS_VERBOSE 260 | print_token(tok); 261 | #endif // LEX_TESTS_VERBOSE 262 | } 263 | 264 | // Restore for the other tests. 265 | lexer::set_default_punctuation_tables(); 266 | } 267 | 268 | static void lex_test_line_count() 269 | { 270 | #if LEX_TESTS_VERBOSE 271 | std::cout << "\nCounting lines in file...\n"; 272 | #endif // LEX_TESTS_VERBOSE 273 | 274 | // Scan as space-delimited strings (only_strings). 275 | lexer lex{ "lex_test_3.txt", lexer::flags::only_strings }; 276 | 277 | int line_count = 0; 278 | std::string line; 279 | 280 | for (; !lex.is_at_end(); ++line_count) 281 | { 282 | line = lex.scan_complete_line(); 283 | 284 | #if LEX_TESTS_VERBOSE 285 | std::cout << "LINE: " << line; 286 | #endif // LEX_TESTS_VERBOSE 287 | } 288 | 289 | #if LEX_TESTS_VERBOSE 290 | std::cout << "Counted " << line_count << " lines.\n"; 291 | #endif // LEX_TESTS_VERBOSE 292 | 293 | assert(line_count == 42); 294 | } 295 | 296 | static void lex_test_word_count() 297 | { 298 | #if LEX_TESTS_VERBOSE 299 | std::cout << "\nCounting words in file...\n"; 300 | #endif // LEX_TESTS_VERBOSE 301 | 302 | // Words will be lexed as identifiers. Punctuations are preserved. 303 | // (only_strings flag would also lex punctuation as identifiers). 304 | lexer lex{ "lex_test_3.txt", lexer::flags::no_string_concat }; 305 | 306 | lexer::token word; 307 | int word_count = 0; 308 | 309 | while (lex.next_token(&word)) 310 | { 311 | if (word.is_identifier()) 312 | { 313 | ++word_count; 314 | } 315 | 316 | #if LEX_TESTS_VERBOSE 317 | print_token(word); 318 | #endif // LEX_TESTS_VERBOSE 319 | } 320 | 321 | #if LEX_TESTS_VERBOSE 322 | std::cout << "Counted " << word_count << " words.\n"; 323 | #endif // LEX_TESTS_VERBOSE 324 | 325 | assert(word_count == 527); 326 | } 327 | 328 | // ======================================================== 329 | // main(): 330 | // ======================================================== 331 | 332 | int main() 333 | { 334 | std::cout << "\nRunning lexer tests...\n"; 335 | 336 | lex_test_scan_num_and_string_values(); 337 | lex_test_scan_matrices(); 338 | lex_test_scan_punctuations(); 339 | lex_test_custom_punct_table(); 340 | lex_test_line_count(); 341 | lex_test_word_count(); 342 | 343 | std::cout << "\nAll tests passed!\n"; 344 | } 345 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------