A set of benchmarks to compare a new prototype MiniZinc implementation
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3/*
4 * Main authors:
5 * Guido Tack <guido.tack@monash.edu>
6 */
7
8/* This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11
12#ifndef __MINIZINC_PARSER_HH__
13#define __MINIZINC_PARSER_HH__
14
15// This is a workaround for a bug in flex that only shows up
16// with the Microsoft C++ compiler
17#if defined(_MSC_VER)
18#define YY_NO_UNISTD_H
19#ifdef __cplusplus
20extern "C" int isatty(int);
21#endif
22#endif
23
24// The Microsoft C++ compiler marks certain functions as deprecated,
25// so let's take the alternative definitions
26#if defined(_MSC_VER)
27#define strdup _strdup
28#define fileno _fileno
29#endif
30
31#if defined(_MSC_VER)
32#pragma warning(disable : 4065)
33#endif
34
35#include <minizinc/astexception.hh>
36#include <minizinc/file_utils.hh>
37#include <minizinc/model.hh>
38#include <minizinc/parser.tab.hh>
39
40#include <algorithm>
41#include <cstdlib>
42#include <cstring>
43#include <iostream>
44#include <map>
45#include <string>
46#include <vector>
47
48namespace MiniZinc {
49
50struct ParseWorkItem {
51 Model* m;
52 IncludeI* ii;
53 std::string dirName;
54 std::string fileName;
55 bool isModelString;
56 ParseWorkItem(Model* m0, IncludeI* ii0, const std::string& dirName0, const std::string& fileName0,
57 bool isModelString0 = false)
58 : m(m0), ii(ii0), dirName(dirName0), fileName(fileName0), isModelString(isModelString0) {}
59};
60
61/// %State of the %MiniZinc parser
62class ParserState {
63public:
64 ParserState(const std::string& f, const std::string& b, std::ostream& err0,
65 std::vector<ParseWorkItem>& files0, std::map<std::string, Model*>& seenModels0,
66 MiniZinc::Model* model0, bool isDatafile0, bool isFlatZinc0, bool parseDocComments0)
67 : filename(f.c_str()),
68 buf(b.c_str()),
69 pos(0),
70 length(static_cast<unsigned int>(b.size())),
71 lineStartPos(0),
72 nTokenNextStart(1),
73 hadNewline(false),
74 files(files0),
75 seenModels(seenModels0),
76 model(model0),
77 isDatafile(isDatafile0),
78 isFlatZinc(isFlatZinc0),
79 parseDocComments(parseDocComments0),
80 hadError(false),
81 err(err0) {}
82
83 const char* filename;
84
85 void* yyscanner;
86 const char* buf;
87 unsigned int pos, length;
88
89 int lineStartPos;
90 int nTokenNextStart;
91 bool hadNewline;
92
93 std::vector<ParseWorkItem>& files;
94 std::map<std::string, Model*>& seenModels;
95 MiniZinc::Model* model;
96
97 bool isDatafile;
98 bool isFlatZinc;
99 bool parseDocComments;
100 bool hadError;
101 std::vector<SyntaxError> syntaxErrors;
102 std::ostream& err;
103
104 std::string stringBuffer;
105
106 void printCurrentLine(int firstCol, int lastCol) {
107 const char* eol_c = strchr(buf + lineStartPos, '\n');
108 if (eol_c) {
109 if (eol_c == buf + lineStartPos) return;
110 err << std::string(buf + lineStartPos, eol_c - (buf + lineStartPos));
111 } else {
112 err << buf + lineStartPos;
113 }
114 err << std::endl;
115 for (int i = 0; i < firstCol - 1; i++) err << " ";
116 for (int i = firstCol; i <= lastCol; i++) err << "^";
117 err << std::endl;
118 }
119
120 int fillBuffer(char* lexBuf, unsigned int lexBufSize) {
121 if (pos >= length) return 0;
122 int num = std::min(length - pos, lexBufSize);
123 memcpy(lexBuf, buf + pos, num);
124 pos += num;
125 return num;
126 }
127};
128
129Model* parse(Env& env, const std::vector<std::string>& filename,
130 const std::vector<std::string>& datafiles, const std::string& textModel,
131 const std::string& textModelName, const std::vector<std::string>& includePaths,
132 bool ignoreStdlib, bool parseDocComments, bool verbose, std::ostream& err);
133
134Model* parseFromString(Env& env, const std::string& model, const std::string& filename,
135 const std::vector<std::string>& includePaths, bool ignoreStdlib,
136 bool parseDocComments, bool verbose, std::ostream& err,
137 std::vector<SyntaxError>& syntaxErrors);
138
139Model* parseData(Env& env, Model* m, const std::vector<std::string>& datafiles,
140 const std::vector<std::string>& includePaths, bool ignoreStdlib,
141 bool parseDocComments, bool verbose, std::ostream& err);
142
143} // namespace MiniZinc
144
145#endif