aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scalam/m/ast/Identifier.scala
blob: 013f641ed92b2ec11d894b8a45e130549efaa213 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package scalam.m.ast

/**
 * A matlab identifier.
 * @param name name of variable (this must be a valid matlab identifier string)
 *
 * @define construct identifier
 */
case class Identifier(name: String) extends Mable {
  def m = name
}

object Identifier {

  def makeValid(raw: String) = {
    val transformSymbols = Map(' ' -> '_').withDefault(c => c)

    val validChars = raw.map(c => transformSymbols(c))

    validChars.headOption match {
      case Some(c) if (!c.isLetter) => 'x' + validChars
      case Some(c) => validChars
      case None => "id"
    }
  }

}