Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (83 sloc) 3.01 KB
//
// cfg - A simple configuration file library.
//
// Copyright (c) 2015-2017 Sean Farrell <sean.farrell@rioki.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#ifndef _CFG_PARSER_H_
#define _CFG_PARSER_H_
#include <string>
#include <iostream>
namespace cfg
{
class Config;
class Parser
{
public:
Parser(Config& config);
~Parser();
/**
* Parse a stream.
*
* @param in the stream to parse
* @param file the file that is parsed, for error purposes
* @param line the initial line
**/
void parse(std::istream& in, const std::string& file = "", unsigned int line = 1);
private:
enum Token
{
NO_TOKEN,
WHITESPACE,
OPEN_BRACE, // [
CLOSE_BRACE, // ]
EQUALS, // =
IDENTIFIER, // [a-zA-Z_][a-zA-Z0-9_-]*
STRING, // "([^"]*)"
NUMBER, // (+|-)[0-9]+(\.[0-9]+)
NEWLINE, // \n|(\r\n)|\r
COMMENT, // #[^\n\r]*
FILE_END,
ERROR // this should never be returned
};
Config& config;
std::istream* in;
std::string file;
unsigned int line;
Token next_token;
std::string next_value;
std::string section;
Token get_next_token(std::string& value);
Token lex_token(std::string& value);
Token lex_whitespace(std::string& value);
Token lex_newline(std::string& value);
Token lex_number(std::string& value);
Token lex_identifier(std::string& value);
Token lex_string(std::string& value);
Token lex_comment(std::string& value);
void parse_section();
void parse_section_header();
void parse_value_pair();
void error(const std::string& msg);
Parser(const Parser&) = delete;
const Parser& operator = (const Parser&) = delete;
};
}
#endif