表达式语法

备注

Microsoft Power Fx 是画布应用公式语言的新名称。 当我们从画布应用中提取语言,将其与其他 Microsoft Power Platform 产品集成并使其作为开放源代码提供时,这些文章还在撰写。 从 Microsoft Power Fx 概述开始,了解对此语言的介绍。

Microsoft Power Fx 基于将名称绑定到表达式的公式。 就像在 Excel 工作表中一样,随着表达式的入站依赖项的更改,表达式将重新计算,名称的值也会更改,可能会将重新计算级联到其他公式。

此语法涵盖公式的表达式部分。 绑定到要创建公式的名称依赖于 Power Fx 的集成方式。 在工作表中,绑定语法不公开,它由写入表达式的位置隐含—例如,在 A1 单元格中输入 =B1。 在某些情况下,不需要绑定,Power Fx 将用作表达式计算器,例如在支持数据库表的计算列时。 对于 Power Apps,绑定在具有基于 YAML 的序列化格式以在 Power Apps Studio 外部使用的 Power Apps Studio 中工作时隐含。

语法约定

词法和句法使用文法产生式来表示。 每个文法产生式定义一个非终端符号,以及该非终端符号可能扩展成一系列非终端或终端符号。 在文法产生式中,非终端符号以斜体显示,终端符号以固定宽度字体显示。

文法产生式的第一行是定义的非终端符号的名称,后跟冒号。 每一个后续缩进行都包含一个非终端符号的可能扩展,该非终端符号由一系列非终端或终端符号组成。 例如,产生式:

  GlobalIdentifier:
    [@Identifier]

GlobalIdentifier 定义为包含令牌 [@,后跟标识符,然后是令牌]

当非终端符号有多个可能的扩展时,替代项在单独的行中列出。 下标“opt”用于指示可选符号。 例如,产生式:

  FunctionCall:
    FunctionIdentifier(FunctionArgumentsopt)

为以下项的简写:

  FunctionCall:
    FunctionIdentifier()
    FunctionIdentifier(FunctionArguments)

替代项通常在单独的行中列出,但在有许多替代项的情况下,“one of”一词可能出现在单行上给出的一列扩展之前。 这只是对在单独的行中列出每个替代项的简化。

例如,产生式:

  DecimalDigit:one of
    0123456789

为以下项的简写:

  DecimalDigit:
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

词法分析

lexical-unit 产生式定义了 Power Fx 表达式的词法语法。 每个有效的 Power Fx 表达式都遵循此语法。

  ExpressionUnit:
    ExpressionElementsopt

  ExpressionElements:
    ExpressionElement
    ExpressionElementExpressionElementsopt

  ExpressionElement:
    空格
    注释

在词法级别,Power Fx 表达式由一系列空格注释令牌元素组成。 以下各节将介绍这些产生式。 在句法语法中,只有令牌元素是有意义的。

空白

空格用于分隔 Power Apps 文档中的注释和令牌。

  空格
    任何 Unicode 空格分隔符(类 Zs)
    任何 Unicode 行分隔符(类 Zl)
    任何 Unicode 段落分隔符(类 Zp)
    水平制表符 (U+0009)
    换行符 (U+000A)
    垂直制表符 (U+000B)
    换页符 (U+000C)
    回车符 (U+000D)
    换行符 (U+0085)

注释

支持两种形式的注释:

  • 单行注释以字符 // 开头,并扩展到源行的末尾。
  • 分隔注释以字符 /* 开头,以字符 */ 结尾。 分隔注释可以跨多行。

  注释
    DelimitedComment
    SingleLineComment

  SingleLineComment:
    //SingleLineCommentCharactersopt

  SingleLineCommentCharacters:
    SingleLineCommentCharacter
    SingleLineCommentCharacterSingleLineCommentCharactersopt

  SingleLineCommentCharacter:
    除 NewLineCharacter 之外的任何 Unicode 字符

  DelimitedComment:
    /*DelimitedCommentCharactersopt*/

  DelimitedCommentCharacters:
    DelimitedCommentCharactersNoAsteriskDelimitedCommentCharactersopt
    *DelimitedCommentAfterAsteriskCharacters

  DelimitedCommentAfterAsteriskCharacters:
    DelimitedCommentNoSlashAsteriskCharacterDelimitedCommentCharactersopt
    *DelimitedCommentAfterAsteriskCharacters

  DelimitedCommentCharactersNoAsterisk:
    除 *(星号)之外的任何 Unicode 字符

  DelimitedCommentNoSlashAsteriskCharacter:
    除 /(斜杠)或 *(星号)之外的任何 Unicode 字符

注释不嵌套。 字符序列 /**/ 在单行注释中没有特殊含义,字符序列 ///* 在分隔注释中没有特殊含义。

注释不在文本文字字符串中处理。

以下示例包含两个分隔注释:

/* Hello, world
*/
"Hello, world"    /* This is an example of a text literal */

以下示例包含三个单行注释:

// Hello, world
//
"Hello, world"    // This is an example of a text literal

文字

文字是值的源代码表示形式。

  Literal:
    LogicalLiteral
    NumberLiteral
    TextLiteral

逻辑文字

逻辑文字用于写入值 true 和 false,并生成逻辑值。

  LogicalLiteral:one of
    truefalse

数字文字

数字文字用于写入数值并生成数值。

  NumberLiteral:
    DecimalDigitsExponentPartopt
    DecimalDigitsDecimalSeparatorDecimalDigitsoptExponentPartopt
    DecimalSeparatorDecimalDigitsExponentPartopt

  DecimalDigits:
    DecimalDigit
    DecimalDigitsDecimalDigit

  DecimalDigit:one of
    0123456789

  ExponentPart:
    ExponentIndicatorSignoptDecimalDigits

  ExponentIndicator:one of
    eE

  Sign:one of
    +-

文本文字

文本文字用于写入 Unicode 字符序列并生成文本值。 文本文字用双引号括起来。 要在文本值中包含双引号,重复双引号,如以下示例所示:

"The ""quoted"" text" // The "quoted" text

  TextLiteral:
    "TextLiteralCharactersopt"

  TextLiteralCharacters:
    TextLiteralCharacterTextLiteralCharactersopt

  TextLiteralCharacter:
    TextCharacterNoDoubleQuote
    DoubleQuoteEscapeSequence

  TextCharacterNoDoubleQuote:
    除双引号之外的任何 Unicode 代码点

  DoubleQuoteEscapeSequence:
    ""

标识符

标识符是用于引用值的名称。 标识符可以是常规标识符,也可以是带单引号的标识符。

  Identifier:
    IdentifierNamebutnotOperatororContextKeyword

  IdentifierName:
    IdentifierStartCharacterIdentifierContinueCharactersopt
    'SingleQuotedIdentifier'

  IdentifierStartCharacter:
    LetterCharacter
    _

  IdentifierContinueCharacter:
    IdentifierStartCharacter
    DecimalDigitCharacter
    ConnectingCharacter
    CombiningCharacter
    FormattingCharacter

  IdentifierContinueCharacters:
    IdentifierContinueCharacterIdentifierContinueCharactersopt

  LetterCharacter:
    类大写字母 (Lu) 或小写字母 (Ll) 的任何 Unicode 字符
    类标题大写字母 (Lt) 的任何 Unicode 字符
    类字母修饰符 (Lm) 或字母其他 (Lo) 的任何 Unicode 字符
    类数字字母 (Nl) 的任何 Unicode 字符

  CombiningCharacter:
    类非空格标记 (Mn) 或空格组合标记 (Mc) 的任何 Unicode 字符

  DecimalDigitCharacter:
    类小数位数 (Nd) 的任何 Unicode 字符

  ConnectingCharacter:
    类连接器标点符号 (Pc) 的任何 Unicode 字符

  FormattingCharacter:
    类格式 (Cf) 的任何 Unicode 字符

带单引号的标识符

加单引号的标识符包含 Unicode 字符的任何序列用作标识符,包括关键字、空格、注释和运算符。 单引号字符使用两个单引号的转义序列来支持。

  SingleQuotedIdentifier:
    SingleQuotedIdentifierCharacters

  SingleQuotedIdentifierCharacters:
    SingleQuotedIdentifierCharacterSingleQuotedIdentifierCharactersopt

  SingleQuotedIdentifierCharacter:
    TextCharactersNoSingleQuote
    SingleQuoteEscapeSequence

  TextCharactersNoSingleQuote:
    除 ' (U+0027) 之外的任何 Unicode 字符

  SingleQuoteEscapeSequence:
    ''

消除歧义标识符

  DisambiguatedIdentifier:
    TableColumnIdentifier
    GlobalIdentifier

  TableColumnIdentifier:
    Identifier[@Identifier]

  GlobalIdentifier:
    [@Identifier]

上下文关键字

  ContextKeyword:
    Parent
    Self
    ThisItem
    ThisRecord

区分大小写

Power Apps 标识符区分大小写。 编写公式时,创作工具会自动将它们更改为正确的大小写。

分隔符

  DecimalSeparator:
    .(点)用于使用点作为小数分隔符的语言,例如,1.23
    ,(逗号)用于使用逗号作为小数分隔符的语言,例如,1,23

  ListSeparator:
    ,(逗号),如果 DecimalSeparator.(点)
    ;(分号),如果 DecimalSeparator,(逗号)

  ChainingSeparator:
    ;(分号),如果 DecimalSeparator.(点)
    ;;(双分号),如果 DecimalSeparator,(逗号)

运算符

在公式中使用运算符来描述涉及一个或多个操作数的操作。 例如,表达式 a + b 使用 + 运算符添加两个操作数 ab

  Operator:
    BinaryOperator
    BinaryOperatorRequiresWhitespace
    PrefixOperator
    PrefixOperatorRequiresWhitespace
    PostfixOperator

  BinaryOperator:one of
    =<<=>>=<>
    +-*/^
    &
    &&||
    inexactin

  BinaryOperatorRequiresWhitespace:
    And空格
    Or空格

  PrefixOperator:
    !

  PrefixOperatorRequiresWhitespace:
    Not空格

  PostfixOperator:
    %

引用运算符

  ReferenceOperator:one of
    .!

对象引用

  Reference:
    BaseReference
    BaseReferenceReferenceOperatorReferenceList

  BaseReference:
    Identifier
    DisambiguatedIdentifier
    ContextKeyword

  ReferenceList:
    Identifier
    IdentifierReferenceOperatorReferenceList

内联记录

  InlineRecord:
    {InlineRecordListopt}

  InlineRecordList:
    Identifier:Expression
    Identifier:ExpressionListSeparatorInlineRecordList

内联表

  InlineTable:
    [InlineTableListopt]

  InlineTableList:
    Expression
    ExpressionListSeparatorInlineTableList

Expression

  Expression:
    Literal
    Reference
    InlineRecord
    InlineTable
    FunctionCall
    (Expression)
    PrefixOperatorExpression
    ExpressionPostfixOperator
    ExpressionBinaryOperatorExpression

链式表达式

  ChainedExpression:
    Expression
    ExpressionChainingSeparatorChainedExpressionopt

函数调用

  FunctionCall:
    FunctionIdentifier(FunctionArgumentsopt)

  FunctionIdentifier:
    Identifier
    Identifier.FunctionIdentifier

  FunctionArguments:
    ChainedExpression
    ChainedExpressionListSeparatorFunctionArguments