User avatar
Lead Software Architect
Posts: 377
Joined: Fri Sep 14, 2012 12:20 pm
Contact: Website

Writing your Own Parser

Thu Oct 02, 2014 6:21 pm

When an input is received all the existing parsers are called to generate a Data that are fed to functions inside gears as arguments.

If a Data is generated its then added in the caller Context for evaluation. In the following example code we create a parser that will extract Car Company Names and create a CarCompany data that can be later used by a function inside a Gear.

Writing your own parser is simple, lets see the code.

CSharp Code

using Syn.Engine;
namespace MyPlugin
public class MyPlugin : SynPlugin
public override void Initialize()
this.Engine.AddParser(new CarCompanyParser());

public class CarCompany : SynData
public string CompanyName { get; set; }

public class CarCompanyParser : SynParser
public override void Parse(SynCaller caller)
string companiesPattern = "\b(tata|volkswagen|dodge|lamborghini|renault|toyota)\b";

foreach (SynMatch match in SynHelper.GetMatches(caller.Message, companiesPattern))
CarCompany company = new CarCompany();
company.CompanyName = match.Value;
caller.Context.AddData(company, match);

Visual Basic Code

Imports Syn.Engine

Public Class MyPlugin
Inherits SynPlugin

Public Overrides Sub Initialize()
Me.Engine.AddParser(New CarCompanyParser)
End Sub

Public Class CarCompany : Inherits SynData
Public Property CompanyName As String
End Class

Public Class CarCompanyParser : Inherits SynParser
Public Overrides Sub Parse(caller As SynCaller)
Dim companiesPattern As String = "b(tata|volkswagen|dodge|lamborghini|renault|toyota)b"

For Each match As SynMatch In SynHelper.GetMatches(caller.Message, companiesPattern)
Dim company As New CarCompany
company.CompanyName = match.Value
caller.Context.AddData(company, match)
End Sub
End Class

End Class
In the above code there's this function called "Parse" - this function is called automatically by the Engine when a user message is received.

In the above example we test a pattern against the message and for each pattern match we add an item ( a newly defined SynData named CarCompany )

Please note that when you add a Data to the Context provide the context with the exact Match Item ( it shouldn't be null ) - this is necessary for the Engine to perform some calculations when submitting this Data as a arguments to other functions.

Return to “Archived (Old Posts)”

Who is online

Users browsing this forum: No registered users and 1 guest