Class: Ruco::Ruco
Instance Attribute Summary
Attributes inherited from Production
#prodset, #prodtype
Instance Method Summary
collapse
Methods inherited from Group
#convert_thing, #either, #generate, #grammar_tree, #group, #group_type, #many, #maybe, #maybemany, #multiply, #one, #sync
Constructor Details
#initialize(grammar_name, &block) ⇒ Ruco
Returns a new instance of Ruco.
229
230
231
232
233
234
235
|
# File 'lib/ruco.rb', line 229
def initialize(grammar_name, &block)
@name = grammar_name
super(grammar_name)
@productions = {grammar_name => self}
@custom_tokens = {}
instance_eval(&block)
end
|
Instance Method Details
#generate_atg ⇒ Object
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
# File 'lib/ruco.rb', line 744
def generate_atg()
productionlist = []
productiondecl = []
@productions.each do |prodname, prod|
decllist = []
prod.prodset.each do |key, prodinfo|
if prodinfo[:type] == :id
decllist << "(. #{key}Ptr #{key.downcase}; .)"
end
end
declarations = decllist.join "\n"
attributes = "<#{prodname}Ptr& production>" unless prodname == @name
constructor = ""
constructor = "(. production = std::make_shared<class #{prodname}>(); .)" if prod.prodtype == :normal
linecolreg = "(. unsigned curline = la->line, curcol = la->col; .)"
recordlinecol = "(. production->_line = curline; production->_col = curcol; .)"
production_string = <<-PRODUCTION
#{prodname}#{attributes} = #{constructor}
#{linecolreg}
#{declarations}
#{prod.generate}
#{recordlinecol}
.
PRODUCTION
production_string.gsub!(/production/, @name.downcase) if prodname == @name
productionlist << production_string
end
productions = productionlist.join("\n")
custom_tokens = @custom_tokens.map do |k,v|
"\t#{k} = #{v}."
end.join("\n")
<<-FRAMEEND
#include <iostream>
#include <memory>
#include "#{@name}.hpp"
/*
WARNING: This file is generated using ruco. Please modify the .ruco file if you wish to change anything
https://github.com/davidsiaw/ruco
*/
COMPILER #{@name}
#{@name}Ptr #{@name.downcase};
CHARACTERS
bigletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
letter = "abcdefghijklmnopqrstuvwxyz".
underscore = "_".
digit = "0123456789".
cr = '\\r'.
lf = '\\n'.
tab = '\\t'.
stringCh = ANY - '"' - '\\\\' - cr - lf.
charCh = ANY - '\\'' - '\\\\' - cr - lf.
printable = '\\u0020' .. '\\u007e'.
hex = "0123456789abcdef".
TOKENS
pascalcase = bigletter { bigletter | letter | digit }.
camelcase = letter { bigletter | letter | digit }.
number = digit { digit } [ '.' digit { digit } ].
hexinteger = '0' 'x' hex { hex }.
string = '"' { stringCh | '\\\\' printable } '"'.
badString = '"' { stringCh | '\\\\' printable } (cr | lf).
char = '\\'' ( charCh | '\\\\' printable { hex } ) '\\''.
endOfLine = cr | lf.
#{custom_tokens}
PRAGMAS
ddtSym = '$' { digit | letter }.
optionSym = '$' letter { letter } '='
{ digit | letter
| '-' | '.' | ':'
}.
COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO lf
IGNORE tab + cr + lf
/*-------------------------------------------------------------------------*/
PRODUCTIONS
#{productions}
END #{@name}.
FRAMEEND
end
|
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# File 'lib/ruco.rb', line 301
def ()
classlist = []
parent_map = {}
dependency_hash = TsortableHash.new
@productions.each do |prodname, prod|
if prod.prodtype == :normal
prod.prodset.each do |key, prodinfo|
if prodinfo[:type] == :id
if !dependency_hash[prodname]
dependency_hash[prodname] = []
end
dependency_hash[prodname] << "#{key}"
end
end
elsif prod.prodtype == :variation
prod.prodset.each do |key, prodinfo|
if prodinfo[:type] == :id
if !dependency_hash[key]
dependency_hash[key] = []
end
dependency_hash[key] << "#{prodname}"
if !parent_map[key]
parent_map[key] = []
end
parent_map[key] << "#{prodname}"
end
end
end
end
@productions.each do |prodname, prod|
if !dependency_hash[prodname]
dependency_hash[prodname] = []
end
end
dependency_hash.tsort.each do |prodname|
prod = @productions[prodname]
memberlist = []
if prod.prodtype == :normal
prod.prodset.each do |key, prodinfo|
name = "#{key}"
if prodinfo[:type] == :id
if prodinfo[:count] > 1
memberlist << "#{name}Array #{name.downcase.pluralize};"
else
memberlist << "#{name}Ptr #{name.downcase};"
end
elsif prodinfo[:type] == :token
memberlist << "std::wstring content;"
end
end
elsif prod.prodtype == :variation
enumeration_list = []
prod.prodset.each do |key, prodinfo|
enumeration_list << "\t#{key.upcase}_#{prodname.upcase}"
end
enumerations = enumeration_list.join ",\n"
classlist << <<-TYPE_ENUM_CONTENT
enum #{prodname}Type
{
#{enumerations}
};
TYPE_ENUM_CONTENT
memberlist << "virtual #{prodname}Type get_#{prodname.downcase}_type() const = 0;"
else
puts "UNKNOWN PRODUCTION TYPE: #{prod.prodtype}"
end
parent_declaration = ""
parent_impl = ""
if parent_map[prodname]
list = parent_map[prodname].map{|x| "public #{x}"}.join ", "
parent_declaration = ": #{list}"
parent_map[prodname].each do |parent|
memberlist << <<-PARENTDECL
virtual #{parent}Type get_#{parent.downcase}_type() const
{
return #{prodname.upcase}_#{parent.upcase};
}
PARENTDECL
end
end
members = memberlist.map {|x| "\t#{x}"}.join "\n"
classlist << <<-CLASSCONTENT
class #{prodname} #{parent_declaration}
{
public:
unsigned _line, _col;
#{members}
};
typedef std::shared_ptr<#{prodname}> #{prodname}Ptr;
typedef std::vector<#{prodname}Ptr> #{prodname}Array;
CLASSCONTENT
end
classes = classlist.join "\n"
= <<-HEADEREND
#ifndef #{@name.upcase}_HPP
#define #{@name.upcase}_HPP
/*
WARNING: This file is generated using ruco. Please modify the .ruco file if you wish to change anything
https://github.com/davidsiaw/ruco
*/
#include <string>
#include <memory>
#include <vector>
namespace #{@name}
{
#{classes}
}
#endif // #{@name.upcase}_HPP
HEADEREND
end
|
#generate_libcpp ⇒ Object
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
# File 'lib/ruco.rb', line 635
def generate_libcpp()
functions = ""
function_declarations = ""
@productions.each do |prodname, prod|
f = write_jsonify_function(prodname, prod)
functions += f[:definition]
function_declarations += <<-FUNCDECLEND
#{f[:name]};
FUNCDECLEND
end
<<-CPPCONTENT
#include "parse_#{@name.downcase}.hpp"
/*
WARNING: This file is generated using ruco. Please modify the .ruco file if you wish to change anything
https://github.com/davidsiaw/ruco
*/
namespace #{@name}
{
#{@name}Ptr Parse(std::string sourceFile)
{
std::shared_ptr<FILE> fp (fopen(sourceFile.c_str(), "r"), fclose);
if (!fp)
{
throw FileNotFoundException();
}
std::shared_ptr<Scanner> scanner (new Scanner(fp.get()));
std::shared_ptr<Parser> parser (new Parser(scanner.get()));
parser->Parse();
return parser->#{@name.downcase};
}
#{function_declarations}
#{functions}
picojson::value Jsonify(#{@name}Ptr parseResult)
{
return picojson::value(Compile#{@name}(parseResult));
}
}
CPPCONTENT
end
|
#generate_libhpp ⇒ Object
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/ruco.rb', line 470
def generate_libhpp()
<<-HPPCONTENT
#ifndef PARSE_#{@name.upcase}_HPP
#define PARSE_#{@name.upcase}_HPP
/*
WARNING: This file is generated using ruco. Please modify the .ruco file if you wish to change anything
https://github.com/davidsiaw/ruco
*/
#include <iostream>
#include <memory>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include "Scanner.h"
#include "Parser.h"
#include "picojson.hpp"
namespace #{@name}
{
/**
* Exception thrown when the specified source file is not found
*/
class FileNotFoundException {};
/**
* Parses a source file into the data structure of #{@name}
*/
#{@name}Ptr Parse(std::string sourceFile);
/**
* Transforms the data structure of #{@name} to an abstract syntax tree in JSON format
*/
picojson::value Jsonify(#{@name}Ptr parseResult);
}
#endif // PARSE_#{@name.upcase}_HPP
HPPCONTENT
end
|
#generate_makefile ⇒ Object
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
# File 'lib/ruco.rb', line 687
def generate_makefile
<<-MAKEFILEEND
V = 0
AT_0 := @
AT_1 :=
REDIR_0 := > /dev/null
REDIR_1 :=
AT = $(AT_$(V))
REDIR = $(REDIR_$(V))
CXXFLAGS=-g -Wall -std=gnu++11
LDFLAGS=
TARGET=#{@name.downcase}_parse
SRCDIR=.
SRC=$(shell find $(SRCDIR) -name "*.cpp")
OUT=obj
OBJS=$(SRC:%.cpp=$(OUT)/%.o)
DEPS=$(SRC:%.cpp=$(OUT)/%.d)
NODEPS=clean
.PHONY = all clean
.SECONDEXPANSION:
all: $(TARGET)
$(AT)echo "Build complete"
clean:
$(AT)-rm -rf $(TARGET) $(OUT)
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
-include $(DEPS)
endif
$(TARGET): $(OBJS) $(LUA)
$(AT)echo [LINK] $@
$(AT)$(CXX) $(CXXFLAGS) $^ $(LDFLAGS) -o $@
%/.f:
$(AT)echo [MKDIR] $(dir $@)
$(AT)mkdir -p $(dir $@)
$(AT)touch $@
$(OUT)/%.d:%.cpp $$(@D)/.f
$(AT)echo [DEP] $<
$(AT)$(CXX) $(CXXFLAGS) -MM -MT '$(patsubst %.d,%.o,$@)' $< -MF $@
$(OUT)/%.o:%.cpp $(OUT)/%.d
$(AT)echo [C++] $<
$(AT)$(CXX) $< $(CXXFLAGS) -c -o $@
.PRECIOUS: %/.f
MAKEFILEEND
end
|
#generate_tree ⇒ Object
297
298
299
|
# File 'lib/ruco.rb', line 297
def generate_tree
result = @productions.map{|k,v| [k, v.grammar_tree] }.to_h
end
|
#grammar(name, &block) ⇒ Object
237
238
239
240
241
|
# File 'lib/ruco.rb', line 237
def grammar(name, &block)
p = Production.new(name, &block)
p.instance_eval(&block)
@productions[name] = p
end
|
#token(name, type, definitionstring = nil) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/ruco.rb', line 261
def token(name, type, definitionstring=nil)
p = Production.new(name)
case type
when :pascal_case
p.instance_eval do
one Token.new("pascalcase", @prodset)
end
when :camel_case
p.instance_eval do
one Token.new("camelcase", @prodset)
end
when :hex_integer
p.instance_eval do
one Token.new("hexinteger", @prodset)
end
when :number
p.instance_eval do
one Token.new("number", @prodset)
end
when :string
p.instance_eval do
one Token.new("string", @prodset)
end
when :char
p.instance_eval do
one Token.new("char", @prodset)
end
when :custom
@custom_tokens["customToken#{name}"] = "#{definitionstring}"
p.instance_eval do
one Token.new("customToken#{name}", @prodset)
end
end
@productions[name] = p
end
|
#variation(name, *args) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/ruco.rb', line 243
def variation(name, *args)
p = Production.new(name, :variation)
p.instance_eval do
g = Group.new :either, @prodset
g.instance_eval do
args.each do |x|
if x.is_a? Identifier
one Variation.new(x.name)
else
puts "Variation needs to be a grammar"
end
end
end
one g
end
@productions[name] = p
end
|
#write_jsonify_function(prodname, prod) ⇒ Object
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
# File 'lib/ruco.rb', line 518
def write_jsonify_function(prodname, prod)
pset = <<-PSETEND
object[L"_type"] = picojson::value(L"#{prodname}");
object[L"_col"] = picojson::value((double)pointer->_col);
object[L"_line"] = picojson::value((double)pointer->_line);
PSETEND
if prod.prodtype == :variation
switchladder = ""
prod.prodset.each do |key, prodinfo|
switchladder += <<-LADDEREND
case #{key.upcase}_#{prodname.upcase}:
{
content = Compile#{key}(std::dynamic_pointer_cast<#{key}>(pointer));
break;
}
LADDEREND
end
pset += <<-PSETEND
picojson::object content;
switch(pointer->get_#{prodname.downcase}_type())
{
#{switchladder}
}
object[L"_content"] = picojson::value(content);
PSETEND
elsif prod.prodtype == :normal
members_code = ""
prod.prodset.each do |key, prodinfo|
members_code += <<-MEMBERCODEEND
// #{prodinfo}
MEMBERCODEEND
if prodinfo[:count] == 1
if prodinfo[:type] == :token
members_code += <<-PSETEND
object[L"_token"] = picojson::value(pointer->content);
PSETEND
elsif prodinfo[:type] == :id
members_code += <<-PSETEND
if (pointer->#{key.downcase})
{
picojson::object #{key.downcase};
#{key.downcase} = Compile#{key}(pointer->#{key.downcase});
object[L"#{key.downcase}"] = picojson::value(#{key.downcase});
}
PSETEND
end
else
if prodinfo[:type] == :token
raise "Unimplemented"
elsif prodinfo[:type] == :id
members_code += <<-PSETEND
picojson::array #{key.downcase.pluralize};
for(unsigned i=0; i<pointer->#{key.downcase.pluralize}.size(); i++)
{
#{key.downcase.pluralize}.push_back(picojson::value(Compile#{key}(pointer->#{key.downcase.pluralize}[i])));
}
object[L"#{key.downcase.pluralize}"] = picojson::value(#{key.downcase.pluralize});
PSETEND
end
end
end
pset += <<-PSETEND
#{members_code}
PSETEND
end
funcname = "picojson::object Compile#{prodname}(#{prodname}Ptr pointer)"
funcdef = <<-FUNCTIONEND
#{funcname}
{
picojson::object object;
// #{prod.prodtype}
#{pset}
return object;
}
FUNCTIONEND
{name: funcname, definition: funcdef}
end
|