<commit_before>/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  <NAME> <<EMAIL>>
 *  
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "libparse.h"
#include <stdlib.h>
#include <string.h>

#ifndef FILTERLIB
#include "kernel/log.h"
#endif

using namespace PASS_DFFLIBMAP;

std::set<std::string> LibertyAst::blacklist;
std::set<std::string> LibertyAst::whitelist;

LibertyAst::~LibertyAst()
{
	for (auto child : children)
		delete child;
	children.clear();
}

LibertyAst *LibertyAst::find(std::string name)
{
	if (this == NULL)
		return NULL;
	for (auto child : children)
		if (child->id == name)
			return child;
	return NULL;
}

void LibertyAst::dump(FILE *f, std::string indent, std::string path, bool path_ok)
{
	if (whitelist.count(path + "/*") > 0)
		path_ok = true;

	path += "/" + id;

	if (blacklist.count(id) > 0 || blacklist.count(path) > 0)
		return;
	if (whitelist.size() > 0 && whitelist.count(id) == 0 && whitelist.count(path) == 0 && !path_ok) {
		fprintf(stderr, "Automatically added to blacklist: %s\n", path.c_str());
		blacklist.insert(id);
		return;
	}

	fprintf(f, "%s%s", indent.c_str(), id.c_str());
	if (!args.empty()) {
		for (size_t i = 0; i < args.size(); i++)
			fprintf(f, "%s%s", i > 0 ? ", " : "(", args[i].c_str());
		fprintf(f, ")");
	}
	if (!value.empty())
		fprintf(f, " : %s", value.c_str());
	if (!children.empty()) {
		fprintf(f, " {\n");
		for (size_t i = 0; i < children.size(); i++)
			children[i]->dump(f, indent + "  ", path, path_ok);
		fprintf(f, "%s}\n", indent.c_str());
	} else
		fprintf(f, " ;\n");
}

int LibertyParser::lexer(std::string &str)
{
	int c;

	do {
		c = fgetc(f);
	} while (c == ' ' || c == '\t' || c == '\r');

	if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.') {
		str = c;
		while (1) {
			c = fgetc(f);
			if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.')
				str += c;
			else
				break;
		}
		ungetc(c, f);
		// fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
		return 'v';
	}

	if (c == '"') {
		str = c;
		while (1) {
			c = fgetc(f);
			if (c == '\n')
				line++;
			str += c;
			if (c == '"')
				break;
		}
		// fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
		return 'v';
	}

	if (c == '/') {
		c = fgetc(f);
		if (c == '*') {
			int last_c = 0;
			while (c > 0 && (last_c != '*' || c != '/')) {
				last_c = c;
				c = fgetc(f);
				if (c == '\n')
					line++;
			}
			return lexer(str);
		}
		ungetc(c, f);
		// fprintf(stderr, "LEX: char >>/<<\n");
		return '/';
	}

	if (c == '\\') {
		c = fgetc(f);
		if (c == '\r')
			c = fgetc(f);
		if (c == '\n')
			return lexer(str);
		ungetc(c, f);
		return '\\';
	}

	if (c == '\n') {
		line++;
		return ';';
	}

	// if (c >= 32 && c < 255)
	// 	fprintf(stderr, "LEX: char >>%c<<\n", c);<commit_msg>Added support for // comments in liberty parser<commit_after>/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2012  <NAME> <<EMAIL>>
 *  
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "libparse.h"
#include <stdlib.h>
#include <string.h>

#ifndef FILTERLIB
#include "kernel/log.h"
#endif

using namespace PASS_DFFLIBMAP;

std::set<std::string> LibertyAst::blacklist;
std::set<std::string> LibertyAst::whitelist;

LibertyAst::~LibertyAst()
{
	for (auto child : children)
		delete child;
	children.clear();
}

LibertyAst *LibertyAst::find(std::string name)
{
	if (this == NULL)
		return NULL;
	for (auto child : children)
		if (child->id == name)
			return child;
	return NULL;
}

void LibertyAst::dump(FILE *f, std::string indent, std::string path, bool path_ok)
{
	if (whitelist.count(path + "/*") > 0)
		path_ok = true;

	path += "/" + id;

	if (blacklist.count(id) > 0 || blacklist.count(path) > 0)
		return;
	if (whitelist.size() > 0 && whitelist.count(id) == 0 && whitelist.count(path) == 0 && !path_ok) {
		fprintf(stderr, "Automatically added to blacklist: %s\n", path.c_str());
		blacklist.insert(id);
		return;
	}

	fprintf(f, "%s%s", indent.c_str(), id.c_str());
	if (!args.empty()) {
		for (size_t i = 0; i < args.size(); i++)
			fprintf(f, "%s%s", i > 0 ? ", " : "(", args[i].c_str());
		fprintf(f, ")");
	}
	if (!value.empty())
		fprintf(f, " : %s", value.c_str());
	if (!children.empty()) {
		fprintf(f, " {\n");
		for (size_t i = 0; i < children.size(); i++)
			children[i]->dump(f, indent + "  ", path, path_ok);
		fprintf(f, "%s}\n", indent.c_str());
	} else
		fprintf(f, " ;\n");
}

int LibertyParser::lexer(std::string &str)
{
	int c;

	do {
		c = fgetc(f);
	} while (c == ' ' || c == '\t' || c == '\r');

	if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.') {
		str = c;
		while (1) {
			c = fgetc(f);
			if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.')
				str += c;
			else
				break;
		}
		ungetc(c, f);
		// fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
		return 'v';
	}

	if (c == '"') {
		str = c;
		while (1) {
			c = fgetc(f);
			if (c == '\n')
				line++;
			str += c;
			if (c == '"')
				break;
		}
		// fprintf(stderr, "LEX: string >>%s<<\n", str.c_str());
		return 'v';
	}

	if (c == '/') {
		c = fgetc(f);
		if (c == '*') {
			int last_c = 0;
			while (c > 0 && (last_c != '*' || c != '/')) {
				last_c = c;
				c = fgetc(f);
				if (c == '\n')
					line++;
			}
			return lexer(str);
		} else if (c == '/') {
			while (c > 0 && c != '\n')
				c = fgetc(f);
			line++;
			return lexer(str);
		}
		ungetc(c, f);
		// fprintf(stderr, "LEX: char >>/<<\n");
		return '/';
	}

	if (c == '\\') {
		c = fgetc(f);
		if (c == '\r')
			c = fgetc(f);
		if (c == '\n')
			return lexer(str);
		ungetc(c, f);
		return '\\';
	}

	if (c == '\n') {
		line++;
		return ';';
	}

	// if (c >= 32 && c < 255)
	// 	fprintf(stderr, "LEX: char >>%c<<\n", c);
{'fraction_non_alphanumeric': 0.22446491465727444, 'fraction_numerical': 0.007179626117583311, 'mean_word_length': 2.939701173959445, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236421', 'n_tokens_mistral': 3244, 'n_tokens_neox': 2925, 'n_words': 828}