Commit 9e192a74 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

splitting class Run into 2 cases: RunInner and RunTerminal

parent 295ad3d9
......@@ -88,7 +88,7 @@
430045FD260496E6000E9898 /* RunFilterMode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43847D7D23F5980C00059835 /* RunFilterMode.cpp */; };
430045FE260496E6000E9898 /* Ranked.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43552ED322A6ADA800C557BA /* Ranked.cpp */; };
430045FF260496E6000E9898 /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
43004600260496E6000E9898 /* KeyS.tpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.tpp */; };
43004600260496E6000E9898 /* KeyS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.cpp */; };
43004601260496E6000E9898 /* KeySI.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43552EDD22A6CFDE00C557BA /* KeySI.cpp */; };
43004602260496E6000E9898 /* KeySIP.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C692A22368A4CE0063ABAE /* KeySIP.cpp */; };
43004603260496E6000E9898 /* KeySIR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439D5228239E576500796CBA /* KeySIR.cpp */; };
......@@ -217,7 +217,7 @@
4315C86C22F9D091003F66AF /* TestRuney.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4315C86B22F9D091003F66AF /* TestRuney.cpp */; };
4315C86D22F9D70C003F66AF /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
4315C86E22F9D70C003F66AF /* Ranked.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43552ED322A6ADA800C557BA /* Ranked.cpp */; };
4315C86F22F9D70C003F66AF /* KeyS.tpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.tpp */; };
4315C86F22F9D70C003F66AF /* KeyS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.cpp */; };
4315C87022F9D828003F66AF /* InputEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43E0E6A3203576A3002D366E /* InputEnv.cpp */; };
43161D81242CBC530079378D /* Voicing.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43161D80242CBC530079378D /* Voicing.cpp */; };
43169949242AA6B6006EA6BE /* MIDIOutput.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43169947242AA6B6006EA6BE /* MIDIOutput.cpp */; };
......@@ -384,6 +384,7 @@
4331FC6A259B320500895F0F /* StaffRange.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4331FC68259B320500895F0F /* StaffRange.cpp */; };
4331FC7F259B737800895F0F /* SingleGraceNote.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4331FC7D259B737800895F0F /* SingleGraceNote.cpp */; };
4332105824FC171600A3A053 /* Root.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4332105724FC171600A3A053 /* Root.cpp */; };
4332790F260E450C00D6AD17 /* KeyS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.cpp */; };
433DA74925EEA62100144F63 /* RewriteMulti.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 433DA74725EEA62100144F63 /* RewriteMulti.cpp */; };
43448D5F25097627007ECBA5 /* ScoreBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43467766247AB20F008A146B /* ScoreBuilder.cpp */; };
43468B442523678E006D45B7 /* WeightDom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43847D7923F55D2A00059835 /* WeightDom.cpp */; };
......@@ -500,7 +501,6 @@
4377138F231FBEE40089153C /* PointedRT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 432F19E3204EE5D8002CD439 /* PointedRT.cpp */; };
43771390231FBF040089153C /* Label.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43979C6222A11762008BA390 /* Label.cpp */; };
437784B923881168007741A2 /* DuraList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C7232F2362E2D800D5E20E /* DuraList.cpp */; };
4377854025C31BF600731C40 /* KeySIP.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C692A22368A4CE0063ABAE /* KeySIP.cpp */; };
437EDA5C20EE419B008F0755 /* Rest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437EDA5A20EE419B008F0755 /* Rest.cpp */; };
437FA6F922F97709003974DC /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
437FC6C120BFFAC900AF0A2D /* InputSegmentMono.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FC6BF20BFFAC900AF0A2D /* InputSegmentMono.cpp */; };
......@@ -568,7 +568,6 @@
4399D57A25233200001A2506 /* RunFilterMode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43847D7D23F5980C00059835 /* RunFilterMode.cpp */; };
439BF4A02053DD5A00E6AA1E /* MusPoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439BF49E2053DD5A00E6AA1E /* MusPoint.cpp */; };
439BF4A12053DD5A00E6AA1E /* MusPoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439BF49E2053DD5A00E6AA1E /* MusPoint.cpp */; };
439D522A239E576500796CBA /* KeySIR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439D5228239E576500796CBA /* KeySIR.cpp */; };
439D522B239E5D0900796CBA /* MRT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4334C5D923732C56007448B7 /* MRT.cpp */; };
439E5D2C257E76F100862F2C /* PitchedNote.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439E5D2A257E76F100862F2C /* PitchedNote.cpp */; };
439EA742230AE49D00DADCFA /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
......@@ -623,7 +622,6 @@
43C67A3325879A2C00FE7724 /* Ambitus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C67A3125879A2B00FE7724 /* Ambitus.cpp */; };
43C6CF1525839F4E002BE3F5 /* PartEng.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C6CF1325839F4E002BE3F5 /* PartEng.cpp */; };
43C723312362E2D800D5E20E /* DuraList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C7232F2362E2D800D5E20E /* DuraList.cpp */; };
43CDB7952577899500956DDF /* KeySIO.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43CDB7932577899500956DDF /* KeySIO.cpp */; };
43CEA93925EFD1E5007C355B /* ValueFile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43CEA93825EFD1E5007C355B /* ValueFile.cpp */; };
43CEA93A25EFD1E5007C355B /* ValueFile.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43CEA93825EFD1E5007C355B /* ValueFile.cpp */; };
43CEBC092029006D000B6F0B /* Point.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43CEBC062029006D000B6F0B /* Point.cpp */; };
......@@ -657,7 +655,7 @@
43CEBE4225DAA750005FA9AE /* Ranked.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43552ED322A6ADA800C557BA /* Ranked.cpp */; };
43CEBE4325DAA750005FA9AE /* RunFilterMode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43847D7D23F5980C00059835 /* RunFilterMode.cpp */; };
43CEBE4425DAA750005FA9AE /* RunFilter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439EA0BA224CCF91006E742E /* RunFilter.cpp */; };
43CEBEF925DAB9A4005FA9AE /* KeyS.tpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.tpp */; };
43CEBEF925DAB9A4005FA9AE /* KeyS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.cpp */; };
43CEBF0825DABA3D005FA9AE /* Event.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4305FB26204FD1C100CCDF10 /* Event.cpp */; };
43CEBF0925DABA3D005FA9AE /* Pitch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3216AECD1EC27F4800F2D04C /* Pitch.cpp */; };
43CEBF0A25DABA3D005FA9AE /* Ambitus.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C67A3125879A2B00FE7724 /* Ambitus.cpp */; };
......@@ -1200,6 +1198,8 @@
4331FC7E259B737800895F0F /* SingleGraceNote.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SingleGraceNote.hpp; sourceTree = "<group>"; };
4332105724FC171600A3A053 /* Root.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Root.cpp; sourceTree = "<group>"; };
4332105924FC172300A3A053 /* Root.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Root.hpp; sourceTree = "<group>"; };
433278E1260DC6CA00D6AD17 /* RunFactory.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = RunFactory.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
433278E2260DC6CA00D6AD17 /* RunFactory.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RunFactory.hpp; sourceTree = "<group>"; };
433322F02008116400D82A0C /* Brecord.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; path = Brecord.tpp; sourceTree = "<group>"; usesTabs = 0; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
433322F12008116400D82A0C /* Brecord.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Brecord.hpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
433322F32008118900D82A0C /* Krecord.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = Krecord.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
......@@ -1347,7 +1347,7 @@
436AD60B1F990380003F03BF /* schema-06.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "schema-06.txt"; sourceTree = "<group>"; };
436F3C0B20ED62F000AC0C65 /* Spiral.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Spiral.cpp; sourceTree = "<group>"; };
436F3C0C20ED62F000AC0C65 /* Spiral.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Spiral.hpp; sourceTree = "<group>"; };
4373B456225163CC00D49D6C /* KeyS.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = KeyS.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
4373B456225163CC00D49D6C /* KeyS.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = KeyS.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
4373B457225163CC00D49D6C /* KeyS.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = KeyS.hpp; sourceTree = "<group>"; };
43770CC22358619A00CC8869 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
43770CC3235861A600CC8869 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
......@@ -2203,12 +2203,14 @@
43770CD1235863A700CC8869 /* CMakeLists.txt */,
437FA6F822F97709003974DC /* ParsingEnv.hpp */,
437FA6F722F97709003974DC /* ParsingEnv.cpp */,
433278E2260DC6CA00D6AD17 /* RunFactory.hpp */,
433278E1260DC6CA00D6AD17 /* RunFactory.tpp */,
43552EE122A6DFCD00C557BA /* Key.hpp */,
43552EE022A6DFCD00C557BA /* Key.tpp */,
43552ED422A6ADA800C557BA /* Ranked.hpp */,
43552ED322A6ADA800C557BA /* Ranked.cpp */,
4373B457225163CC00D49D6C /* KeyS.hpp */,
4373B456225163CC00D49D6C /* KeyS.tpp */,
4373B456225163CC00D49D6C /* KeyS.cpp */,
43552EDE22A6CFDE00C557BA /* KeySI.hpp */,
43552EDD22A6CFDE00C557BA /* KeySI.cpp */,
43C692A32368A4CE0063ABAE /* KeySIP.hpp */,
......@@ -3128,7 +3130,7 @@
430045FD260496E6000E9898 /* RunFilterMode.cpp in Sources */,
430045FE260496E6000E9898 /* Ranked.cpp in Sources */,
430045FF260496E6000E9898 /* ParsingEnv.cpp in Sources */,
43004600260496E6000E9898 /* KeyS.tpp in Sources */,
43004600260496E6000E9898 /* KeyS.cpp in Sources */,
43004601260496E6000E9898 /* KeySI.cpp in Sources */,
43004602260496E6000E9898 /* KeySIP.cpp in Sources */,
43004603260496E6000E9898 /* KeySIR.cpp in Sources */,
......@@ -3293,10 +3295,8 @@
43552ECF22A69C4B00C557BA /* RunFilter.cpp in Sources */,
43847D7E23F5980C00059835 /* RunFilterMode.cpp in Sources */,
439EA742230AE49D00DADCFA /* ParsingEnv.cpp in Sources */,
4332790F260E450C00D6AD17 /* KeyS.cpp in Sources */,
43552EDF22A6CFDE00C557BA /* KeySI.cpp in Sources */,
4377854025C31BF600731C40 /* KeySIP.cpp in Sources */,
439D522A239E576500796CBA /* KeySIR.cpp in Sources */,
43CDB7952577899500956DDF /* KeySIO.cpp in Sources */,
43C723312362E2D800D5E20E /* DuraList.cpp in Sources */,
4377138E231FBEDE0089153C /* RT.cpp in Sources */,
4377138F231FBEE40089153C /* PointedRT.cpp in Sources */,
......@@ -3621,7 +3621,7 @@
43CEBE3625DAA6DF005FA9AE /* SWTA.cpp in Sources */,
43CEBE1A25DAA69B005FA9AE /* SWTAFileIn.cpp in Sources */,
43CEBE4125DAA750005FA9AE /* ParsingEnv.cpp in Sources */,
43CEBEF925DAB9A4005FA9AE /* KeyS.tpp in Sources */,
43CEBEF925DAB9A4005FA9AE /* KeyS.cpp in Sources */,
43CEBE4225DAA750005FA9AE /* Ranked.cpp in Sources */,
43CEBE4325DAA750005FA9AE /* RunFilterMode.cpp in Sources */,
43CEBE4425DAA750005FA9AE /* RunFilter.cpp in Sources */,
......@@ -3847,7 +3847,7 @@
4315C86E22F9D70C003F66AF /* Ranked.cpp in Sources */,
43847D8E23F5AD9600059835 /* RunFilterMode.cpp in Sources */,
438DA6DF22A82EE400194B72 /* RunFilter.cpp in Sources */,
4315C86F22F9D70C003F66AF /* KeyS.tpp in Sources */,
4315C86F22F9D70C003F66AF /* KeyS.cpp in Sources */,
43847D8B23F5AD9600059835 /* KeySI.cpp in Sources */,
43847D8C23F5AD9600059835 /* KeySIP.cpp in Sources */,
43847D8D23F5AD9600059835 /* KeySIR.cpp in Sources */,
......
//
// RunFactory.hpp
// qparse
//
// Created by Florent Jacquemard on 26/03/2021.
// Copyright © 2021 Florent Jacquemard. All rights reserved.
//
#ifndef RunFactory_hpp
#define RunFactory_hpp
#include <stdio.h>
#include <assert.h>
#include <stack> // std::stack
//#include <queue> // std::queue
#include "State.hpp"
#include "SymbLabel.hpp"
#include "Transition.hpp"
#include "SWTA.hpp"
#include "ParsingEnv.hpp"
#include "Runey.hpp"
#include "RunInner.hpp"
#include "RunTerm.hpp"
namespace Parsing{
/// methods for the generation of runs common to several key classes K.
/// Every Run object represents an augmented transition built by extension of
/// - a transitios of a base WTA, for the parsing inside a bar, or
/// - a generic transition for parsing over the bars, built in this factory.
///
/// The form of the parse tree is the following:
/// ```
/// B2
/// / \
/// m1 B2
/// / \
/// m2 ...
/// B2
/// / \
/// mk B0
/// ```
/// where B0, B2 are nullary (final) and binary bar symbols
/// and m1,...,mk are the parse tree for measures 1 to k.
template<class K>
class RunFactory
{
public:
// RunFactory(const K& k);
/// @brief construct runs targeting complete instances of
/// this key.
/// @param si a stack to be filled with inner runs. must be empty.
/// @param st a stack to be filled with pairs containing terminal runs.
/// must be empty.
/// @param env parsing environment, containing a wta.
// @return how many runs have been added to the stacks.
/// @warning the runs constructed are added to the given stacks.
/// - si will contain inner runs, partial or complete.
/// - st will contains pairs made of
/// - a complete instance k' of k if k is partial, and NULL otherwise.
/// - a complete terminal run targeting r'
/// All the runs in stacks must be deallocate elsewhere.
static void runs(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
ParsingEnv& env);
/// add to the stacks runs corresponding to the extension
/// of a base WTA transition.
/// @param si a stack to be filled with inner runs.
/// @param st a stack to be filled with pairs containing terminal runs.
/// @param env parsing environment, containing a wta.
static void runsWTA(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
const ParsingEnv& env);
/// construct base "over-the-bar transitions" (nullary and binary),
/// extend them and add them to the stacks.
static void runsBar(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
ParsingEnv& env);
/// generic method adding to the given stack one innterminal run
/// built from the params.
/// @param k target of the run added. must be cmoplete.
/// @param t transition to build terminal runs. must be terminal and complete.
/// @param lab given label. see RunTerm.
/// @param w weight of run. must not be unknown. see RunTerm.
/// @param len number of input points (events) parsed by this run.
/// see RunTerm.
/// @param st a stack to be filled with pairs containing terminal runs.
/// @warning default case only for complete keys.
static void terminalRun(const K& k,
const Transition& t,
label_t lab,
const Weight& w,
size_t len,
std::stack<std::pair<const K*, RunTerm<K>*>>& st);
/// generic method adding to the given stack one inner runs built from the
/// given transition.
/// The run added is partial (empty), and is built from the transition
/// solely (same label and weight as the transition).
/// @param t transition to build inner runs. must be inner and complete.
/// @param si a stack to be filled with inner runs.
/// @warning the runs constructed are added to the given stack.
/// They must be deallocate elsewhere.
static void innerRun(const K& k,
const Transition& t,
std::stack<RunInner<K>*>& si);
/// unique nullary outside-the-bar base transition targeting the embedded state.
/// @param env parsing environment. may be modified.
static const Transition& barTransition0(state_t state, ParsingEnv& env);
/// unique binary outside-the-bar base transition targeting the embedded state.
/// @param env parsing environment. may be modified.
static const Transition& barTransition2(state_t state, ParsingEnv& env);
/// allocate and return a special Run representing a parse failure for this key.
/// @param env parsing environment.
/// @return a pointer to a newly allocated FAIL run, with
/// - transition with dummy symbol, empty body, weight zero in the
/// domain of the base wta in the passed environment.
/// - fail symbol
/// - weight zero (in the domain of the base wta).
/// This returned run will answer succesfully to Run.fail().
static RunTerm<K>* failRun(const K& k,
const ParsingEnv& env);
private:
// const K& _key;
//
// inline const K& key() { return _key; }
// inline const state_t state() { return _key.state(); }
};
} // end namespace Parsing
// separated definition of template class
#include "RunFactory.tpp"
#endif /* RunFactory_hpp */
/// @}
//
// RunFactory.tpp
// qparse
//
// Created by Florent Jacquemard on 26/03/2021.
// Copyright © 2021 Florent Jacquemard. All rights reserved.
//
namespace Parsing{
//template<class K>
//void RunFactory::RunFactory(const K& k):
//_key(k)
//{ }
template<class K>
void RunFactory<K>::runs(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
ParsingEnv& env)
{
assert(si.empty());
assert(st.empty());
if (State::isWTA(k.state()))
runsWTA(k, si, st, env);
else if (State::isMeta(k.state()))
runsBar(k, si, st, env);
else
{
ERROR("RunFactory: unexpected state {}", k.state());
}
}
template<class K>
void RunFactory<K>::runsWTA(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
const ParsingEnv& env)
{
assert(State::isWTA(k.state()));
assert(env.wta);
assert(env.wta->isRegistered(k.state()));
// read transitions of the base WTA.
for (Tlist<Transition>::const_iterator
i = env.wta->cbegin(k.state());
i != env.wta->cend(k.state()); i++)
{
const Transition& t = *i;
assert(t.size() <= t.arity()); // may differ in case of multiplicity
// let the key class extend each transition and add extension to stack
if (t.terminal())
{
k.RunsTerminal(t, st, env);
}
else if (t.inner())
{
k.RunsInner(t, si, env);
}
else // should not happen
{
ERROR("RunFactory: unexpected WTA transition type {}", t);
}
}
}
template<class K>
void RunFactory<K>::runsBar(const K& k,
std::stack<RunInner<K>*>& si,
std::stack<std::pair<const K*, RunTerm<K>*>>& st,
ParsingEnv& env)
{
assert(k.complete());
assert(State::isMeta(k.state()));
// create final bar nullary transition. it is added to the env pool.
const Transition& t0 = barTransition0(k.state(), env);
// let the key class extend this transition and add extension to stack
k.RunsTerminal(t0, st, env);
// RunTerm<K>* r0 = new RunTerm<K>(t0, 0); // empty interval
// st.push(std::make_pair((K*) NULL, r0)); // only for complete keys
// create bar binary transition. it is added to the env pool.
const Transition& t2 = barTransition2(k.state(), env);
// let the key class extend this transition and add extension to stack
k.RunsInner(t2, si, env);
// innerRuns(k, t2, si);
}
// default
template<class K>
void RunFactory<K>::terminalRun(const K& k,
const Transition& t,
label_t a,
const Weight& w,
size_t len,
std::stack<std::pair<const K*, RunTerm<K>*>>& st)
{
assert(k.complete()); // ONLY FOR COMPLETE KEY !
assert(t.arity() == SymbLabel::arity(a));
//const Weight& w = t.weight(); // weight(a, t, env);
if (w.zero()) // skip.
{
WARN("RunFactory: weight zero for term. transition {}, skip", t);
return;
}
RunTerm<K>* r = new RunTerm<K>(t, a, w, len);
TRACE("NEW terminal run {} -> {} for {}", *r, k, t);
st.push(std::make_pair(&k, r)); // this key is complete
}
template<class K>
void RunFactory<K>::innerRun(const K& k,
const Transition& t,
std::stack<RunInner<K>*>& si)
{
assert(t.inner());
assert(t.arity() == SymbLabel::arity(t.label()));
RunInner<K>* r = new RunInner<K>(t);
TRACE("NEW empty inner run {} -> {} for {}", *r, k, t);
assert(r->arity() == t.arity());
assert(r->size() == 0);
assert(r->label() == t.label());
si.push(r);
}
template<class K>
const Transition& RunFactory<K>::barTransition0(state_t state, ParsingEnv& env)
{
assert(State::isMeta(state));
assert(env.pool);
if (env.pool->isRegistered(state))
{
// we assume that there are exactly 1 nullary and 1 binary
// transitions targeting _state in env. pool.
for (Tlist<Transition>::const_iterator
i = env.pool->cbegin(state);
i != env.pool->cend(state); i++)
{
if (i->arity() == 0)
return *i;
}
ERROR("KeyFactory:barTransition0: transition not found in pool, state {}",
state);
throw std::runtime_error("barTransition0 not found\n");
}
else
{
const Weight tone = env.pool->weight_one();
Tlist<Transition>& tl = env.pool->add(state, (state == -1));
tl.emplace(SymbLabel::make_bar(0), tone);
assert(! tl.empty());
Transition& t = tl.last();
TRACE("KeyFactory: add BAR0 transition to pool: {} -> {}", state, t);
return t;
}
}
template<class K>
const Transition& RunFactory<K>::barTransition2(state_t state, ParsingEnv& env)
{
assert(State::isMeta(state));
assert(env.pool);
if (env.pool->isRegistered(state))
{
// we assume that there are exactly 1 nullary and 1 binary
// transitions targeting _state in env. pool.
for (Tlist<Transition>::const_iterator
i = env.pool->cbegin(state);
i != env.pool->cend(state); i++)
{
if (i->arity() == 2)
return *i;
}
ERROR("KeyFactory:barTransition2: transition not found in pool, state {}",
state);
throw std::runtime_error("barTransition2 not found\n");
}
else
{
const Weight tone = env.pool->weight_one();
Tlist<Transition>& tl = env.pool->add(state, (state == -1));
tl.emplace(SymbLabel::make_bar(2), tone);
assert(! tl.empty());
Transition& t = tl.last();
assert(env.wta);
t.push(env.wta->initial());
t.push(state - 1); /// @todo macro next Bar state
TRACE("KeyFactory: add BAR2 transition to pool: {} -> {}", state, t);
return t;
}
}
template<class K>
RunTerm<K>* RunFactory<K>::failRun(const K& k,
const ParsingEnv& env)
{
assert(env.wta);
const Transition& t0 = env.dummyTransition_zero();
assert(t0.weight().zero());
size_t length = 0; // number of input symbols parsed
label_t a = SymbLabel::make_fail(length);
return new RunTerm<K>(t0, a, t0.weight(), length);
}
} // end namespace Parsing
......@@ -26,6 +26,13 @@
namespace Parsing{
template<class K>
class RunInner;
template<class K>
std::ostream& operator<<(std::ostream& o, const RunInner<K>& r);
/// An inner run is a particular case of run with at least one subrun.
/// An inner run is complete when the length of the list of children is the arity.
/// It is partial otherwise.
......@@ -82,8 +89,8 @@ public:
bool operator==(const RunInner<K>& rhs) const;
/// @return current number of children for this run.
/// it is the arity - multiplicity if this run is complete.
/// it is < arity - multiplicity if this run is partial.
/// it can be smaller than the arity in case of multiplicities.
/// it can be smaller than the number of expected children when this run is partial.
virtual size_t size() const;
/// @return this run has currently no children.
......@@ -99,14 +106,14 @@ public:
/// @param i the index of the subrun, between 0 and size()-1.
/// @return the ith subrun or NULL if there is none.
/// @warning this run must be complete.
virtual const Run<K>* subrun(size_t i) const;
const Run<K>* subrun(size_t i) const;
/// return the ith key of this run.
/// @param i index of subrun, must be between 0 and run size - 1.
const K* key(size_t i) const;
friend std::ostream& operator<<(std::ostream& o, const RunInner<K>& r);
void print(std::ostream& o) const;
protected:
/// pointers (keys) to children sub-runs.
......@@ -140,6 +147,14 @@ private:
};
template<class K>
std::ostream& operator<<(std::ostream& o, const RunInner<K>& r)
{
r.print(o);
return o;
}
} // end namespace Parsing
// separated definition of template class
......
......@@ -235,11 +235,11 @@ const K* RunInner<K>::key(size_t i) const
template<class K>
std::ostream& operator<<(std::ostream& o, const RunInner<K>& r)
void RunInner<K>::print(std::ostream& o) const
{
SymbLabel::print(r._label, o);
SymbLabel::print(this->_label, o);
if (r._children.empty())
if (_children.empty())
{
o << "()";
}
......@@ -247,18 +247,17 @@ std::ostream& operator<<(std::ostream& o, const RunInner<K>& r)
{
o << "(";
typename std::vector<Record<K>*>::const_iterator
i = r._children.cbegin();
assert(i != r._children.cend());
i = _children.cbegin();
assert(i != _children.cend());
o << *((*i)->key());
++i;
for(; i != r._children.cend(); ++i)