Path: blob/master/dep/reshadefx/include/effect_lexer.hpp
4246 views
/*1* Copyright (C) 2014 Patrick Mours2* SPDX-License-Identifier: BSD-3-Clause3*/45#pragma once67#include "effect_token.hpp"89namespace reshadefx10{11/// <summary>12/// A lexical analyzer for C-like languages.13/// </summary>14class lexer15{16public:17explicit lexer(18std::string input,19bool ignore_comments = true,20bool ignore_whitespace = true,21bool ignore_pp_directives = true,22bool ignore_line_directives = false,23bool ignore_keywords = false,24bool escape_string_literals = true,25const location &start_location = location()) :26_input(std::move(input)),27_cur_location(start_location),28_ignore_comments(ignore_comments),29_ignore_whitespace(ignore_whitespace),30_ignore_pp_directives(ignore_pp_directives),31_ignore_line_directives(ignore_line_directives),32_ignore_keywords(ignore_keywords),33_escape_string_literals(escape_string_literals)34{35_cur = _input.data();36_end = _cur + _input.size();37}3839lexer(const lexer &lexer) { operator=(lexer); }40lexer &operator=(const lexer &lexer)41{42_input = lexer._input;43_cur_location = lexer._cur_location;44reset_to_offset(lexer._cur - lexer._input.data());45_end = _input.data() + _input.size();46_ignore_comments = lexer._ignore_comments;47_ignore_whitespace = lexer._ignore_whitespace;48_ignore_pp_directives = lexer._ignore_pp_directives;49_ignore_keywords = lexer._ignore_keywords;50_escape_string_literals = lexer._escape_string_literals;51_ignore_line_directives = lexer._ignore_line_directives;5253return *this;54}5556/// <summary>57/// Gets the current position in the input string.58/// </summary>59size_t input_offset() const { return _cur - _input.data(); }6061/// <summary>62/// Gets the input string this lexical analyzer works on.63/// </summary>64/// <returns>Constant reference to the input string.</returns>65const std::string &input_string() const { return _input; }6667/// <summary>68/// Performs lexical analysis on the input string and return the next token in sequence.69/// </summary>70/// <returns>Next token from the input string.</returns>71token lex();7273/// <summary>74/// Advances to the next token that is not whitespace.75/// </summary>76void skip_space();77/// <summary>78/// Advances to the next new line, ignoring all tokens.79/// </summary>80void skip_to_next_line();8182/// <summary>83/// Resets position to the specified <paramref name="offset"/>.84/// </summary>85/// <param name="offset">Offset in characters from the start of the input string.</param>86void reset_to_offset(size_t offset);8788private:89/// <summary>90/// Skips an arbitrary amount of characters in the input string.91/// </summary>92/// <param name="length">Number of input characters to skip.</param>93void skip(size_t length);9495void parse_identifier(token &tok) const;96bool parse_pp_directive(token &tok);97void parse_string_literal(token &tok, bool escape);98void parse_numeric_literal(token &tok) const;99100std::string _input;101location _cur_location;102const std::string::value_type *_cur, *_end;103104bool _ignore_comments;105bool _ignore_whitespace;106bool _ignore_pp_directives;107bool _ignore_line_directives;108bool _ignore_keywords;109bool _escape_string_literals;110};111}112113114